skip to Main Content

This is the problem and, why am I not getting output as Today is date(24/01/2024) pls resolve this, and explain why am I not getting date in the below command ?

[email protected]:~$ date | echo "Today is"
Today is

I tried this but couldnot resolve this, help me.

2

Answers


  1. This is arguably off-topic for SO, but I’m treating it as a programming question about the shell.

    You’re feeding the date as input to echo, but echo doesn’t do anything with its input; it’s completely ignored. You can send anything you like into it and all you get back is whatever you put on the command line:

    $ echo 'boo!' | echo hi
    hi
    $ echo hello </etc/passwd
    hello
    

    etc, etc, etc.

    You can do what you want in a number of ways. For instance, you could simply echo the Today is first and then call date afterward:

    $ printf '%s ' 'Today is'; date
    Today is Tue Jan 23 22:43:01 EST 2024
    

    But most solutions would use command substitution to include the output of date as a string inside the command doing the echoing. For example:

    $ printf 'Today is %sn' "$(date)"
    Today is Tue Jan 23 22:43:53 EST 2024
    

    The "$(date)" construct runs date and is then replaced by whatever it outputs.

    I used printf instead of echo, which is a best practice for shell programs; the behavior of echo is slightly inconsistent between shell implementations, and most of them don’t strictly adhere to the POSIX specification. printf is much more consistent, and the use of format specifiers means you don’t have to do as much interpolation, which otherwise might require mixing and matching types of quotation marks. You do have to supply your own newlines, but that also makes it easy to echo stuff without a newline, as in the first solution above.

    Login or Signup to reply.
  2. try : echo Today is $(date +%d/%m/%Y)
    It dosen’t work cause the a | b operator feed the a command with the b result
    You need to print "Today is" followed by the result of the date command wich you format then

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search