Introduction to the shell
The command interpreter is the interface between the user and the operating system, hence the name "shell".
The shell therefore acts as an intermediary between the operating system and the user thanks to command lines that are entered by the latter. Its role consists in reading the command line, interpreting its meaning, carry out the command, and then return the result via the outputs.
The shell is an executable file responsible for interpreting the commands, transmitting them to the system, and returning the result. Ther are several shells, the most common being sh (called "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 upon opening of a command prompt. The default shell is specified in the dans 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 by simply executing the corresponding executable file, for example:
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 whose name starts with a dot, located in the basic user directory, i.e. /home/user_name/.configuration_file). Then, a prompt is displayed as follows:
By default, for most shells, the prompt consists of the name of the machine, followed by two points (:), the current directory, then a character indicating the type of user connected:
- "$" speciies a normal user
- "#" specifies the administrator, called "root"
The command line concept
A command line is a chain of characters representing a command which corresponds to an executable file of the system or rather a shell command as well as optional arguments (parameters):
ls -al /home/jf/
In the above command, ls
is the aame of the command, -al
are arguments. Arguments beginning with -
are called options
. Generally, for each command, there is a certain number of options which can be detailed by typing one of the following commands:
Once a command is run, a process is created. This process opens three flows:
- stdin, called standard input, in which case the process reads the input data. By default, stdin refers to the keyboard; STDIN is identified by the number 0;
- stdout, called standard output, in which case the process writes the output data. By default, stdin refers to the screen; STDOUT is identified by the number 1;
- stderr, called standard error, in which case the process writes the error messages. By default, stderr refers to the screen. STDERR is identified by the number 2;
By default, whenever a program is run, the data are therefore read from the keyboard, and the program sends its output and its errors to the screen. However, it is also possible to read data from any input device, even from a file, and to send the output to a display device, a file, etc.
Like any Unix type system, Linux has mechanisms which make it possible to redirect the standard input-output to files.
Using the character ">" therefore 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. In case a file already exists with the same name, such file will be deleted. The following command simply creates an empty 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 an essentially similar manner, the character "<" indicates a redirection of the standard input. The following command sends the content of the file toto.txt with the command cat, whose only purpose 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 chain located to the right is found. In the following example, the standard input is read until the word STOP is found, and then, the result is displayed:
cat << STOP
The pipes are a special communication mechanism of all UNIX systems. A piple, symbolised by a vertical bar (character "|"), makes it possible to assign the standard output of a command to the standard input of another, like a pipe allow communication between the standard input of a 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 result 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 files of the current directory, selects the lines containing the word "zip" (thanks to the command grep), and counts the total number of lines:
ls -l | grep zip | wc -l