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
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
, butecho
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: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 calldate
afterward:But most solutions would use command substitution to include the output of
date
as a string inside the command doing the echoing. For example:The
"$(date)"
construct runsdate
and is then replaced by whatever it outputs.I used
printf
instead ofecho
, which is a best practice for shell programs; the behavior ofecho
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.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