UNIX system - The shell

Introduction to the shell

The command interpreter is the interface between the user and the operating system, hence its name "shell".

The shell therefore acts as an intermediary between the operating system and the user using command lines entered by the latter. Its role consists of reading the command line, interpreting its meaning, executing the command, and then returning the result via the outputs.

The shell is an executable file responsible for interpreting commands, transmitting them to the system, and returning the result. There are several shells, the most common being sh (called the "Bourne shell"), bash ("Bourne again shell"), csh ("C Shell"), Tcsh ("Tenex C shell"), ksh ("Korn shell"),and zsh ("Zero shell"). Their name generally matches the name of the executable.

Each user has a default shell, which will be launched when a command prompt is opened. The default shell is specified in the configuration file /etc/passwd in the last field of the line corresponding to the user. It is possible to change the shell during a session simply by executing the corresponding executable file, for example:

/bin/bash

Command prompt window (prompt)

The shell is initialized by reading its overall configuration (in a file of the directory /etc/), followed by reading the user's own configuration (in a hidden file the name of which starts with a dot, located in the basic user directory, i.e. /home/user_name/.configuration_file). Then, a command prompt window or prompt is displayed as follows:

machine:/directory/current$

By default, for most shells, the prompt consists of the name of the machine, followed by a colon (:), the current directory, then a character indicating the type of user connected:

"$" specifies a normal user

"#" specifies the administrator, called "root"

The command line concept

A command line is a character string representing a command corresponding to an executable system file or shell command along with optional arguments (parameters):

ls -al /home/jf/

In the above command, ls is the name of the command, -al and /home/jean-francois/ are arguments. Arguments beginning with - are called options. Generally, for each command there are a certain number of options which can be detailed by entering one of the following commands:

command --help
command -?
man command

Standard input-output

Once a command is run, a process is created. This process then opens three flows:

stdin, called the standard input, where the process will read the input data. By default stdin refers to the keyboard; STDIN is identified by the number 0;

stdout, called standard output, where the process will write the output data. By default, stdin refers to the screen; STDOUT is identified by the number 1;

stderr, called standard error, where the process will write error messages. By default, stderr refers to the screen. STDERR is identified by the number 2;

By default, whenever a program is run data is read from the keyboard and the program sends its output and errors to the screen. However, it is also possible to read data from any input device, even a file, and send the output to a display device, a file, etc.

Redirections

Like any Unix type system, Linux has mechanisms which make it possible to redirect the standard input-output to files.

So, using the ">" character makes it possible to redirect the standard output of a command on the left to the file located on the right:

ls -al /home/jf/ > toto.txt
echo "Toto" > /etc/myconfigurationfile

The following command is equivalent to a copy of the files:

cat toto > toto2

The purpose of the ">" redirection is to create a new file. So, if a file with the same name already exists it will be deleted. The following command simply creates an empty file:

> file

Using the double character ">>" makes it possible to add the standard output to the file, i.e. add the output after the file without deleting it.

In the same way, the "<" character indicates a redirection of the standard input. The following command sends the content of the toto.txt file to the input of the command cat, the only purpose of which is to display the content on the standard output (example not useful, but instructive):

cat < toto.txt

Finally, using the "<<" redirection makes it possible to read on the standard input, until the string located to the right is found. In the following example, the standard input will be read until the word STOP is found, and then the result will be displayed:

cat << STOP

Communication pipes

Pipes are a communication mechanism specific to all UNIX systems. A pipe, symbolised by a vertical bar ("|" character), makes it possible to assign the standard output of one command to the standard input of another, like a pipe enabling communication between the standard input of one command with the standard output of another one.

In the following example, the standard output of the command ls -al is sent to the program sort, which is responsible for sorting the results in alphabetical order:

ls -al | sort

This makes it possible to connect a certain number of commands through successive pipes. In the example below, the command displays all the files in the current directory, selects the lines containing the word "zip" (using the grep command), and counts the total number of lines: