15

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"
  • 6
    $ man date && man echo at the shell. – code_dredd Feb 8 '17 at 2:19
74
$ 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
  • 20
    This is an advanced RTFM answer, +1 – YSC Feb 7 '17 at 16:12
  • 1
    I'd say this is missing an explanation for what the plus sign before the argument is – Délisson Junio Feb 7 '17 at 16:30
  • @DélissonJunio edited, but Melebius' answer shows it better with the direct man page quote – Zanna Feb 7 '17 at 16:33
  • @Zanna I'm not the OP, but what catched my eye was the fact that the plus seems weird as I'd think bash would interpret everything between quotes as one argument, not including the plus. But I guess this is another question – Délisson Junio Feb 7 '17 at 16:34
  • 2
    You can actually put the plus inside the quotes as well. Bash has an odd-feeling way of running together quoted and adjacent text, e.g. cd "$x"/output or even cd nospace/"with space"/nospace – StarWeaver Feb 8 '17 at 13:50
10

You should start with manual pages, the command man. Just type man <command> to get information about a <command>. Navigating in man is not very intuitive but there are lots of guides to it, for example https://wiki.gentoo.org/wiki/Man_page/Navigate#Navigating_and_searching_man_pages.

Relevant parts of man echo and man date:

echo

echo [SHORT-OPTION]... [STRING]...

Echo the STRING(s) to standard output.

-n     do not output the trailing newline

So it prints the string and does not go to the new line after that (which is the default behavior), so the output of the next command will be printed on the right side of the echoed string.

date

date [OPTION]... [+FORMAT]

FORMAT controls the output.  Interpreted sequences are:

%A     locale's full weekday name (e.g., Sunday)

%B     locale's full month name (e.g., January)

%d     day of month (e.g., 01)

%Y     year

By default, date pads numeric fields with zeroes.  The following optional flags may follow '%':

-      (hyphen) do not pad the field

I hope it’s clear. Feel free to ask if not.

  • Thanks a lot. I have another question: Is there already a built-in function to show date in terminal? – Unknown person Feb 7 '17 at 7:43
  • @Zanna Its not actually a new question, it is related to a question that I have asked. – Unknown person Feb 7 '17 at 7:52
  • 1
    @Unknownperson if you want the date to be displayed in the terminal by default, that is really a completely different question to "what does this code do" because you would need to edit the PS1 variable (or perhaps it can be done with terminal app settings - about that I am ignorant) which is out of the scope of this post – Zanna Feb 7 '17 at 7:54
  • 1
    @Unknownperson date does show date in terminal (command-line interface). If you want something more (like what Zanna suggests), please ask a new question. – Melebius Feb 7 '17 at 7:55
4
echo -n "Today's date is: "

It will print: Today's date is:

date +"%A, %B %-d, %Y"

It will print something like this : Tuesday, February 7, 2017

Advice :

  1. use man or --help command to know more about any other commands.
    eg :

    man echo
    echo --help
    
  2. Try these challenges challenges are really good to help you learn to do things on the terminal.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.