Basic Terminal Skills
The shell is the default working environment for a Linux administrator. It is the environment where users and administrators enter commands that are executed by the operating system. Different shells for Linux are available, but bash is the common shell. So when we are talking about “the shell” in this documentation, we are actually talking about the bash shell. This chapter provides an overview of some of the items that you will encounter when working with the shell.
The purpose of the Linux shell is that it provides an environment in which commands can be executed. The shell takes care of interpreting the command that a user has entered correctly. To do this, the shell makes a difference between three kinds of commands:
- Internal Commands
- External Commands
An alias is a command that a user can define as needed. Some aliases are provided by default; typealiason the command line to get an overview. To define an alias, use
alias newcommand='oldcommand' , as in the default alias
ll='ls -l --color=auto'. Aliases are executed before anything else.
An internal command is a command that is a part of the shell itself. It is available when the shell is loaded and can be executed from memory without any lookup from disk. An external command is a command that exists as an executable file on disk of the computer. Because it has to be read from disk, it is a bit slower. When a user executes a command, the shell first looks to determine whether it is an internal command; if it is not, it looks for an executable file with a name that matches the command on disk. To find out whether a command is a bash internal, or an executable file on disk, you can use the type command.
To look up external commands, the $PATH variable is used. This variable defines a list of directories that is searched for a matching filename when a user enters a command. To find out which exact command the shell will be using, you can use the
which command. For instance, type
which ls to find out where the shell will get the
ls command from.
You should notice that for security reasons that the current directory is not in the PATH variable and that Linux does not look in the current directory to see whether a specific command is available from that directory. That is why you need to start a command that is in the current directory but nowhere in the $PATH by including ./ in front of it. The dot stands for the current directory, and by running it as ./, you’ll tell bash to look for the command in the current directory.
The $PATH variable can be set for specific users, but in general, most users will be using the same PATH variable. The only exception to this is the user root, who needs access to specific administration commands.
By default when a command is executed it shows its results on the screen of the computer you are working on. The computer monitor is the so-called standard output, which is also referred to as the STDOUT. The shell also has default destinations to send error messages to and to accept input. Table below gives an overview of all three.
|Name||Default Destination||Use in Redirection||File Descriptor Number|
|STDIN||Computer Keyboard||< (same as 0<)||0|
|STDOUT||Computer Monitor||> (same as 1>)||1|
So if you run a command, that command would expect input from the keyboard, and it would normally send its output to the monitor of your computer without making a difference between normal output and errors. Some commands, however, are started at the background and not from a current terminal session, so these commands do not have a monitor or console session to send their output to, and they do not listen to keyboard input to accept their standard input. That is where redirection comes in handy.
Programs started from the command line have no idea what they are reading from or writing to. They just read from channel (file descriptor) 0 if they want to read from standard input, and they write to file descriptor number 1 to display output and to file descriptor 2 if they have error messages to be output. By default, these are connected to the keyboard and the screen. If you use redirection symbols such as <, >, and |, the shell connects the file descriptors to files or other commands. We first look at < and >. Later we discuss pipes (using the | symbol). Table below shows the most common redirectors that are used from the bash shell.
|> (same as 1>||Redirects STDOUT. If redirection is to a file, the current contents of that file are overwritten.|
|>> (same as 1>>)||Redirects STDOUT. If output is written to a file, the output is appended to that file.|
|2>&1||Redirects STDERR to the same destination as STDOUT.|
|< (same as 0<)||Redirects STDIN.|
In I/O redirection, files can be used to replace the default STDIN, STDOUT, and STDERR. You can also redirect todevice files. A device file on Linux is a file that is used to access specific hardware. Your hard disk for instance can be referred to as / dev/sda, the console of your server is known as /dev/console or /dev/tty1, and if you want to discard a commands output, you can redirect to /dev/null. Notice that to access most device files you need to be root.
Where an I/O redirector is used to use alternatives for keyboard and computer monitor, a pipe can be used to catch the output of one command and use that as input for a second command. If a user runs the command
ls, for instance, the output of the command is shown on screen. If the user uses
ls | less, the commands ls and less are started in parallel. The standard output of the ls command is connected to the standard input of less. Everything that ls writes to the standard output will become available for read from standard input in less. The result is that the output of ls is shown in a pager, where the user can browse up and down through the results easily.
As a Linux administrator, you’ll use pipes a lot. Using pipes makes Linux a flexible operating system; by combining multiple commands using pipes, you can create kind of super commands that make almost anything possible.
A convenient feature of the bash shell is the bash history. Bash is configured to keep the last 1,000 commands you have used (and if shell session is never closed, the exact number can grow even much beyond that). When a shell session is closed, the history of that session is updated to the history file. The name of this file is .bash_ history, and it is created in the home directory of the user who started a specific shell session. Notice that the history file is closed only when the shell session is closed; until that moment, all commands in the history are kept in memory.
The history feature makes it easy to repeat complex commands. There are several ways of working with history:
historyto show a list of all commands in the bash history.
- Use Ctrl+R to open the prompt from which you can do backward searches in commands that you have previously used. Just type a part of the command you are looking for, and it will be displayed automatically. Use Ctrl+R to search further backward based on the same search criteria.
- Type !number to execute a command with a specific number from history.
- Type !sometext to execute the last command that starts with sometext. Notice that this is a potentially dangerous command because the command that was found is executed immediately!
Another useful feature of the bash shell is automatic completion. This feature helps you in finding the command you need, and it also works on variables and filenames, and on some occasions even within command shells that are opened.
Bash completion is used most on commands. Just type the beginning of a command and press the Tab key on your computer’s keyboard. If there is only one option for completion, bash will complete the command automatically for you. If there are several options, you need to press the Tab key once more to get an overview of all the available options.