Ubuntu – What does this syntax mean

bashcommand linedatescriptssyntax

I am new to terminal and trying to learn how to use it.

What do these lines do? And how do they work?

echo -n "Today's date is: "
date +"%A, %B %-d, %Y"

Best Answer

$ type echo
echo is a shell builtin

meaning, the echo command is part of the bash program itself (assuming you use bash)

-n is an option, so let's see what it does

$ help echo
Write arguments to the standard output
-n  do not append a newline

So when we run the line:

zanna@monster:~$ echo -n "Today's date is: "
Today's date is: zanna@monster:~$ 

Hmm that doesn't look very good, because there's no newline after the printed text. We'll come back to this.

$ type date
date is /bin/date

ah so the date command is a separate program. What does it do?

$ man date
Display the current time in the given FORMAT, or set the system date.

The characters after the date command are format options (which must be preceded by +) - different parts of the date are specified (for example %A is the full name of the day of the week - see the rest of man date for the complete list of options)

$ date +"%A, %B %-d, %Y"
Tuesday, February 7, 2017

So if we put the commands together in a script and then run the script we will get

Today's date is: Tuesday, February 7, 2017

Nice! If you want the same effect in a terminal, you can use a semicolon to separate the two commands instead of a newline:

$ echo -n "Today's date is: " ; date +"%A, %B %-d, %Y"
Today's date is: Tuesday, February 7, 2017
Related Question