2. Bash Basics

Bash operates in different modes: It executes programs and performs interactive sessions (both called shell). So, the login command starts an interactive session after successful authentication, as well as each opened terminal. The short key [STRG]+[ALT]+[T] starts a terminal in Ubuntu Linux.

Terminal with interactive Bash session
Figure 1: Terminal with interactive Bash session

The command bash starts an interactive Bash session explicit from command line. Each command has to be confirmed by the [ENTER] key:

$ bash

After invoking an interactive session, Bash prints the prompt "$" to the terminal and waits for user input from keyboard. After hitting the [ENTER] key Bash parses the input into a sequence of commands and executes them:

$ ls
bin  datas  notes.dat  texts

The input is parsed into one token: "ls". This token is interpreted as the command ls.

$ ls -a
.  ..  bin  datas  notes.dat  texts

The input is parsed into two tokens: "ls" and "-a". The token "-a" is interpreted as option of the command ls. It treats ls to list entries starting with "." also. Comments starts with "#" and ends on line end. They won't be parsed

$ # comment, not parsed

They are discarded from parsing. The following input:

$ echo "Hello $USER today is"; date

is parsed into four tokens:

"echo" "Hello $USER today is" ";" "date"

The four tokens are evaluated (expanded) before commands are performed and executed. The expression "Hello $USER today is " includes a reference to the variable USER. The value of the variable is expanded in place of the reference $USER. USER stores the login name of the user. After expansion this token becomes:

"Hello pa today is"

Bash does a lot of expansions before performing commands from user input. Expansions are one of the most powerful features of Bash and explained in detail below. After the expansions take place Bash performs two commands: echo "Hello pa today is" and date. The commands were separated by the ";" operator. The commands are executed sequentially within an own environment:

$ echo "Hello $USER today is"; date
Hello pa today is
Wed Oct  3 19:52:47 CEST 2012

Both commands print their output to the so called standard out. By default the standard out is connected with the terminal. But the output may also redirected to a file.

(echo "Hello $User today is"; date) > output

The commands echo and date are programs. They are kept as executable files somewhere inside the directory tree. Bash searches for the programs within all directories listed in the PATH parameter before execution :


":" acts as separator. echo is found at /bin/echo and date at /bin/date in the directory tree.