16

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"
Zanna
  • 69,223
  • 56
  • 216
  • 327
Unknown person
  • 235
  • 3
  • 9

3 Answers3

79
$ 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
terdon
  • 98,183
  • 15
  • 197
  • 293
Zanna
  • 69,223
  • 56
  • 216
  • 327
  • 22
    This is an advanced RTFM answer, +1 – YSC Feb 07 '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 07 '17 at 16:30
  • @DélissonJunio edited, but Melebius' answer shows it better with the direct man page quote – Zanna Feb 07 '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 07 '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 – Weaver Feb 08 '17 at 13:50
11

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.

muru
  • 193,181
  • 53
  • 473
  • 722
Melebius
  • 11,121
  • 8
  • 50
  • 77
  • Thanks a lot. I have another question: Is there already a built-in function to show date in terminal? – Unknown person Feb 07 '17 at 07:43
  • @Zanna Its not actually a new question, it is related to a question that I have asked. – Unknown person Feb 07 '17 at 07: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 07 '17 at 07: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 07 '17 at 07: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.

Zanna
  • 69,223
  • 56
  • 216
  • 327
Avinash
  • 233
  • 2
  • 3
  • 10