2.4.4. Basic shell usage#
Open (a terminal)[sec:open_terminal], if you have not an open terminal already.
Every time you open a terminal, it will automatically run the default shell program,
which is bash on Ubuntu by default.
As explained before, the shell interprets your commands and instructs the kernel to start other programs.
As explained before, Linux systems contains many small, simple commands that do one thing and do it well. We will start exploring the most basic usage of the shell here with some simple examples, starting a few basic programs.
The examples in this section will also illustrate how programs often support more advanced features and configuration options, allowing to alter their behavior. These configuration options can be specified by adding additional options after the command. Sometimes the programmers have put in so many options that itās hard to know which program option gives what effect, so in the next section weāll explore how to look through the built-in manual from within the terminal. Nevertheless, common options of common programs will probably get ingrained into your memory, just by using the terminal often. Just as when you learn a normal language, after some practice you should not need to open the dictionary for every word.
2.4.4.1. Hello terminal!#
As a first example, we will look at the echo command.
As its name suggests, it basically repeats whatever arguments you give it, and prints[1] it back to the terminal:
Letās try it out. Type the following, and donāt forget to press Enter to confirm the command:
$ echo Hello World!
You should see that echo has repeated the words Hello World! in the terminal on a new line after your command.
Once the command has completed, you are presented with a new prompt to enter your next command.
Note
The echo command may seem silly at first sight: why would we want to print something in the terminal that we just typed ourselves a second ago?
As we shall see, echo is often used in scripts that automate shell operations to inform the user of progress or errors.
It can also be used to print the value of variables, just like the āprintā commands in other programming languages,
or be used to save lines of text in a file.
These are all more advanced topics explained later in the manual.
The words after echo are called arguments (characters or numbers following the program name), which act as input for the echo command.
In fact, in this case the echo command has received two separate arguments,
Hello and World!, since bash treats whitespaces (space, tab, newline) as separators between command and each of its arguments.
The echo command prints all of its arguments in order, also separating them with a single space, so we see the same text in the output.
However, the shell ignores any additional whitespace that separates commands and arguments, so if we enter this:
$ echo Hello World!
the echo command will still just get the same two arguments Hello and World!.
Since it prints the arguments one after the other with just a single space,
the resulting Hello World! will not have the same whitespaces as you wrote in the command line.
We can demarcate an argument with double quotes to force the shell to pass an argument as-is, even if it includes spaces or other special characters that the shell might otherwise process differently. Try out this:
$ echo "Hello World!"
Now the echo command receives a single argument, Hello World!, which it prints to the terminal including all the spaces.
Note that the double quotes were not part of the argument itself, so it doesnāt appear in the printed line, they only served to tell bash to include the spaces as part of the input.
Exercise 2.20
How many leading spaces will be printed by each of the following commands? Before you test them, predict what you think the output will be.
$ echo Hello World!
$ echo " Hello World!"
$ echo "Hello World!"
Exercise 2.21
What do you think will happen if you just run echo without any arguments?
Try it out.
2.4.4.2. Show the current date#
Next, letās look at the basic date command, which without arguments tells you what date it is:
$ date
Fri Jul 16 14:04:02 CEST 2010
The output is fairly self-explanatory, except perhaps the CEST part: this indicates itās Central European Standard Time.
Many programs, including date, use optional arguments to let the user set configurable options to alter the programās function once.
On option is set by first having an argument indicating what option to set,
directly followed by an argument that holds the desired value for the option.
The option to set is often indicated by a dash - and a specific single letter (e.g. -a, -R, -d)
or two dashes -- and a specific word (e.g. --help, --force, --all).
Some options act as switches which donāt take an additional value but simply tell the program to operate in a different manner.
One particularly interesting option of date is -d, which should be followed by a value for the option.
You can use this option to tell date not to report what time it is today, but also in the future and the past.
You can even instruct it in more or less plain English:
$ date -d "tomorrow"
Sat Jul 17 14:04:13 CEST 2010
$ date -d "yesterday-1 week"
Thu Jul 8 14:04:18 CEST 2010
This example immediately shows you that what seems like an ordinary little command can actually be quite powerful.
Note
Observe that options usually only affect the program once, so if you run date without any arguments again it
should just print out the current date again, as before.
Options donāt alter the program for subsequent calls.
You will always have to specify all arguments explicitly when you need them.
Exercise 2.22
Use the date command to find out what day it will be in one year, in 13 months and 3 days, and 17 days ago.
Exercise 2.23
Find out what switches date accepts by using the --help switch.
2.4.4.3. A calendar in your terminal#
With date, you can can specify relative dates. However, when you want to know on what date the last Sunday of this month will fall, a calendar is much more useful. In Unix , the cal command can be used to find out such things:
$ cal
July 2010
Su Mo Tu We Th Fr Sa
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
Exercise 2.24
The cal command does not come installed by default in Ubuntu 22.04 LTS,
you can easily install it using the apt command, or with the Ubuntu Software Center. If you need a hint, see Section 2.3.2.
Exercise 2.25
The first argument cal takes is the year. Try cal 2000 and cal 2525. If you specify two arguments, the program understands the first argument to be the month and the second one to be the year.
Find out what day December 31, 7351 is.
In Unix help pages, optional arguments are often indicated by square brackets, like [this].
For the cal command, the syntax then is:
cal [[month] year]
which tells you that you can specify either nothing, or just the year, or the month followed by the year.
Exercise 2.26
What would happen if you would give the year first, then the month? Try this: cal 2010 12.