1.1. Common shell programs
1.1.1. General shell functions
The UNIX shell program interprets user commands, which are either directly entered by the user, or which can be read from a file called the shell script or shell program. Shell scripts are interpreted, not compiled. The shell reads commands from the script line per line and searches for those commands on the system (see Section 1.2
), while a compiler converts a program into machine readable form, an executable file - which may then be used in a shell script.
Apart from passing commands to the kernel, the main task of a shell is providing a user environment, which can be configured individually using shell resource configuration files.
1.1.2. Shell types
Just like people know different languages and dialects, your UNIX system will usually offer a variety of shell types:
- sh or Bourne Shell: the original shell still used on UNIX systems and in UNIX-related environments. This is the basic shell, a small program with few features. While this is not the standard shell, it is still available on every Linux system for compatibility with UNIX programs.
- bash or Bourne Again shell: the standard GNU shell, intuitive and flexible. Probably most advisable for beginning users while being at the same time a powerful tool for the advanced and professional user. On Linux, bash is the standard shell for common users. This shell is a so-called superset of the Bourne shell, a set of add-ons and plug-ins. This means that the Bourne Again shell is compatible with the Bourne shell: commands that work in sh, also work in bash. However, the reverse is not always the case. All examples and exercises in this book use bash.
- csh or C shell: the syntax of this shell resembles that of the C programming language. Sometimes asked for by programmers.
- tcsh or TENEX C shell: a superset of the common C shell, enhancing user-friendliness and speed. That is why some also call it the Turbo C shell.
- ksh or the Korn shell: sometimes appreciated by people with a UNIX background. A superset of the Bourne shell; with standard configuration a nightmare for beginning users.
The file /etc/shells gives an overview of known shells on a Linux system:
mia:~> cat /etc/shells
Your default shell is set in the /etc/passwd file, like this line for user mia
To switch from one shell to another, just enter the name of the new shell in the active terminal. The system finds the directory where the name occurs using the PATH settings, and since a shell is an executable file (program), the current shell activates it and it gets executed. A new prompt is usually shown, because each shell has its typical appearance:
1.2. Advantages of the Bourne Again SHell
1.2.1. Bash is the GNU shell
The GNU project (GNU's Not UNIX) provides tools for UNIX-like system administration which are free software and comply to UNIX standards.
Bash is an sh-compatible shell that incorporates useful features from the Korn shell (ksh) and C shell (csh). It is intended to conform to the IEEE POSIX P1003.2/ISO 9945.2 Shell and Tools standard. It offers functional improvements over sh for both programming and interactive use; these include command line editing, unlimited size command history, job control, shell functions and aliases, indexed arrays of unlimited size, and integer arithmetic in any base from two to sixty-four. Bash can run most sh scripts without modification.
Like the other GNU projects, the bash initiative was started to preserve, protect and promote the freedom to use, study, copy, modify and redistribute software. It is generally known that such conditions stimulate creativity. This was also the case with the bash program, which has a lot of extra features that other shells can't offer.
1.2.2. Features only found in bash
In addition to the single-character shell command line options which can generally be configured using the set
shell built-in command, there are several multi-character options that you can use. We will come across a couple of the more popular options in this and the following chapters; the complete list can be found in the Bash info pages, Bash features->Invoking Bash.
188.8.131.52. Bash startup files
Startup files are scripts that are read and executed by Bash when it starts. The following subsections describe different ways to start the shell, and the startup files that are read consequently.
184.108.40.206.1. Invoked as an interactive login shell, or with `--login'
Interactive means you can enter commands. The shell is not running because a script has been activated. A login shell means that you got the shell after authenticating to the system, usually by giving your user name and password.
- ~/.bash_profile, ~/.bash_login or ~/.profile: first existing readable file is read
- ~/.bash_logout upon logout.
Error messages are printed if configuration files exist but are not readable. If a file does not exist, bash searches for the next.
220.127.116.11.2. Invoked as an interactive non-login shell
A non-login shell means that you did not have to authenticate to the system. For instance, when you open a terminal using an icon, or a menu item, that is a non-login shell.
This file is usually referred to in ~/.bash_profile:
if [ -f ~/.bashrc ]; then . ~/.bashrc; fi
See Chapter 7
for more information on the if
18.104.22.168.3. Invoked non-interactively
All scripts use non-interactive shells. They are programmed to do certain tasks and cannot be instructed to do other jobs than those for which they are programmed.
PATH is not used to search for this file, so if you want to use it, best refer to it by giving the full path and file name.
22.214.171.124.4. Invoked with the sh command
Bash tries to behave as the historical Bourne sh
program while conforming to the POSIX standard as well.
When invoked interactively, the ENV variable can point to extra startup information.
126.96.36.199.5. POSIX mode
This option is enabled either using the set
set -o posix
or by calling the bash
program with the --posix option. Bash will then try to behave as compliant as possible to the POSIX standard for shells. Setting the POSIXLY_CORRECT variable does the same.
188.8.131.52.6. Invoked remotely
Files read when invoked by rshd
[IMG]file:///images/warning.gif[/IMG]Avoid use of r-tools
Be aware of the dangers when using tools such as rlogin
. They are intrinsically insecure because confidential data is sent over the network unencrypted. If you need tools for remote execution, file transfer and so on, use an implementation of Secure SHell, generally known as SSH, freely available from http://www.openssh.org
. Different client programs are available for non-UNIX systems as well, see your local software mirror.
184.108.40.206.7. Invoked when UID is not equal to EUID
No startup files are read in this case.
220.127.116.11. Interactive shells
18.104.22.168.1. What is an interactive shell?
An interactive shell generally reads from, and writes to, a user's terminal: input and output are connected to a terminal. Bash interactive behavior is started when the bash
command is called upon without non-option arguments, except when the option is a string to read from or when the shell is invoked to read from standard input, which allows for positional parameters to be set (see Chapter 3
22.214.171.124.2. Is this shell interactive?
Test by looking at the content of the special parameter -, it contains an 'i' when the shell is interactive:
eddy:~> echo $- himBH
In non-interactive shells, the prompt, PS1, is unset.
126.96.36.199.3. Interactive shell behavior
Differences in interactive mode:
- Bash reads startup files.
- Job control enabled by default.
- Prompts are set, PS2 is enabled for multi-line commands, it is usually set to ">". This is also the prompt you get when the shell thinks you entered an unfinished command, for instance when you forget quotes, command structures that cannot be left out, etc.
- Commands are by default read from the command line using readline.
- Bash interprets the shell option ignoreeof instead of exiting immediately upon receiving EOF (End Of File).
- Command history and history expansion are enabled by default. History is saved in the file pointed to by HISTFILE when the shell exits. By default, HISTFILE points to ~/.bash_history.
- Alias expansion is enabled.
- In the absence of traps, the SIGTERM signal is ignored.
- In the absence of traps, SIGINT is caught and handled. Thus, typing Ctrl+C, for example, will not quit your interactive shell.
- Sending SIGHUP signals to all jobs on exit is configured with the huponexit option.
- Commands are executed upon read.
- Bash checks for mail periodically.
- Bash can be configured to exit when it encounters unreferenced variables. In interactive mode this behavior is disabled.
- When shell built-in commands encounter redirection errors, this will not cause the shell to exit.
- Special built-ins returning errors when used in POSIX mode don't cause the shell to exit. The built-in commands are listed in Section 1.3.2.
- Failure of exec will not exit the shell.
- Parser syntax errors don't cause the shell to exit.
- Simple spell check for the arguments to the cd built-in is enabled by default.
- Automatic exit after the length of time specified in the TMOUT variable has passed, is enabled.
Conditional expressions are used by the [[
compound command and by the test
Expressions may be unary or binary. Unary expressions are often used to examine the status of a file. You only need one object, for instance a file, to do the operation on.
There are string operators and numeric comparison operators as well; these are binary operators, requiring two objects to do the operation on. If the FILE argument to one of the primaries is in the form /dev/fd/N, then file descriptor N is checked. If the FILE argument to one of the primaries is one of /dev/stdin, /dev/stdout or /dev/stderr, then file descriptor 0, 1 or 2 respectively is checked.
Conditionals are discussed in detail in Chapter 7
More information about the file descriptors in Section 8.2.3
188.8.131.52. Shell arithmetic
The shell allows arithmetic expressions to be evaluated, as one of the shell expansions or by the let
Evaluation is done in fixed-width integers with no check for overflow, though division by 0 is trapped and flagged as an error. The operators and their precedence and associativity are the same as in the C language, see Chapter 3
Aliases allow a string to be substituted for a word when it is used as the first word of a simple command. The shell maintains a list of aliases that may be set and unset with the alias
Bash always reads at least one complete line of input before executing any of the commands on that line. Aliases are expanded when a command is read, not when it is executed. Therefore, an alias definition appearing on the same line as another command does not take effect until the next line of input is read. The commands following the alias definition on that line are not affected by the new alias.
Aliases are expanded when a function definition is read, not when the function is executed, because a function definition is itself a compound command. As a consequence, aliases defined in a function are not available until after that function is executed.
We will discuss aliases in detail in Section 3.5
Bash provides one-dimensional array variables. Any variable may be used as an array; the declare
built-in will explicitly declare an array. There is no maximum limit on the size of an array, nor any requirement that members be indexed or assigned contiguously. Arrays are zero-based. See Chapter 10
184.108.40.206. Directory stack
The directory stack is a list of recently-visited directories. The pushd
built-in adds directories to the stack as it changes the current directory, and the popd
built-in removes specified directories from the stack and changes the current directory to the directory removed.
Content can be displayed issuing the dirs
command or by checking the content of the DIRSTACK variable.
More information about the workings of this mechanism can be found in the Bash info pages.
220.127.116.11. The prompt
Bash makes playing with the prompt even more fun. See the section Controlling the Prompt
in the Bash info pages.
18.104.22.168. The restricted shell
When invoked as rbash
or with the --restricted or -r option, the following happens:
- The cd built-in is disabled.
- Setting or unsetting SHELL, PATH, ENV or BASH_ENV is not possible.
- Command names can no longer contain slashes.
- Filenames containing a slash are not allowed with the . (source) built-in command.
- The hash built-in does not accept slashes with the -p option.
- Import of functions at startup is disabled.
- SHELLOPTS is ignored at startup.
- Output redirection using >, >|, ><, >&, &> and >> is disabled.
- The exec built-in is disabled.
- The -f and -d options are disabled for the enable built-in.
- A default PATH cannot be specified with the command built-in.
- Turning off restricted mode is not possible.
When a command that is found to be a shell script is executed, rbash
turns off any restrictions in the shell spawned to execute the script.
1.3. Executing commands
Bash determines the type of program that is to be executed. Normal programs are system commands that exist in compiled form on your system. When such a program is executed, a new process is created because Bash makes an exact copy of itself. This child process has the same environment as its parent, only the process ID number is different. This procedure is called forking
After the forking process, the address space of the child process is overwritten with the new process data. This is done through an exec
call to the system.
mechanism thus switches an old command with a new, while the environment in which the new program is executed remains the same, including configuration of input and output devices, environment variables and priority. This mechanism is used to create all UNIX processes, so it also applies to the Linux operating system. Even the first process, init
, with process ID 1, is forked during the boot procedure in the so-called bootstrapping
1.3.2. Shell built-in commands
Built-in commands are contained within the shell itself. When the name of a built-in command is used as the first word of a simple command, the shell executes the command directly, without creating a new process. Built-in commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities.
Bash supports 3 types of built-in commands:
- Bourne Shell built-ins:
:, ., break, cd, continue, eval, exec, exit, export, getopts, hash, pwd, readonly, return, set, shift, test, [, times, trap, umask and unset.
- Bash built-in commands:
alias, bind, builtin, command, declare, echo, enable, help, let, local, logout, printf, read, shopt, type, typeset, ulimit and unalias.
- Special built-in commands:
When Bash is executing in POSIX mode, the special built-ins differ from other built-in commands in three respects:
The POSIX special built-ins are :, ., break, continue, eval, exec, exit, export, readonly, return, set, shift, trap and unset.
- Special built-ins are found before shell functions during command lookup.
- If a special built-in returns an error status, a non-interactive shell exits.
- Assignment statements preceding the command stay in effect in the shell environment after the command completes.
Most of these built-ins will be discussed in the next chapters. For those commands for which this is not the case, we refer to the Info pages.
1.3.3. Executing programs from a script
When the program being executed is a shell script, bash will create a new bash process using a fork
. This subshell reads the lines from the shell script one line at a time. Commands on each line are read, interpreted and executed as if they would have come directly from the keyboard.
While the subshell processes each line of the script, the parent shell waits for its child process to finish. When there are no more lines in the shell script to read, the subshell terminates. The parent shell awakes and displays a new prompt.
1.4. Building blocks
1.4.1. Shell building blocks
22.214.171.124. Shell syntax
If input is not commented, the shell reads it and divides it into words and operators, employing quoting rules to define the meaning of each character of input. Then these words and operators are translated into commands and other constructs, which return an exit status available for inspection or processing. The above fork-and-exec scheme is only applied after the shell has analyzed input in the following way:
126.96.36.199. Shell commands
- The shell reads its input from a file, from a string or from the user's terminal.
- Input is broken up into words and operators, obeying the quoting rules, see Chapter 3. These tokens are separated by metacharacters. Alias expansion is performed.
- The shell parses (analyzes and substitutes) the tokens into simple and compound commands.
- Bash performs various shell expansions, breaking the expanded tokens into lists of filenames and commands and arguments.
- Redirection is performed if necessary, redirection operators and their operands are removed from the argument list.
- Commands are executed.
- Optionally the shell waits for the command to complete and collects its exit status.
A simple shell command such as touch file1 file2 file3
consists of the command itself followed by arguments, separated by spaces.
More complex shell commands are composed of simple commands arranged together in a variety of ways: in a pipeline in which the output of one command becomes the input of a second, in a loop or conditional construct, or in some other grouping. A couple of examples:
ls | more
gunzip file.tar.gz | tar xvf -
188.8.131.52. Shell functions
Shell functions are a way to group commands for later execution using a single name for the group. They are executed just like a "regular" command. When the name of a shell function is used as a simple command name, the list of commands associated with that function name is executed.
Shell functions are executed in the current shell context; no new process is created to interpret them.
Functions are explained in Chapter 11
184.108.40.206. Shell parameters
A parameter is an entity that stores values. It can be a name, a number or a special value. For the shell's purpose, a variable is a parameter that stores a name. A variable has a value and zero or more attributes. Variables are created with the declare
shell built-in command.
If no value is given, a variable is assigned the null string. Variables can only be removed with the unset
Assigning variables is discussed in Section 3.2
, advanced use of variables in Chapter 10
220.127.116.11. Shell expansions
Shell expansion is performed after each command line has been split into tokens. These are the expansions performed:
- Brace expansion
- Tilde expansion
- Parameter and variable expansion
- Command substitution
- Arithmetic expansion
- Word splitting
- Filename expansion
We'll discuss these expansion types in detail in Section 3.4
Before a command is executed, its input and output may be redirected using a special notation interpreted by the shell. Redirection may also be used to open and close files for the current shell execution environment.
18.104.22.168. Executing commands
When executing a command, the words that the parser has marked as variable assignments (preceding the command name) and redirections are saved for later reference. Words that are not variable assignments or redirections are expanded; the first remaining word after expansion is taken to be the name of the command and the rest are arguments to that command. Then redirections are performed, then strings assigned to variables are expanded. If no command name results, variables will affect the current shell environment.
An important part of the tasks of the shell is to search for commands. Bash does this as follows:
22.214.171.124. Shell scripts
- Check whether the command contains slashes. If not, first check with the function list to see if it contains a command by the name we are looking for.
- If command is not a function, check for it in the built-in list.
- If command is neither a function nor a built-in, look for it analyzing the directories listed in PATH. Bash uses a hash table (data storage area in memory) to remember the full path names of executables so extensive PATH searches can be avoided.
- If the search is unsuccessful, bash prints an error message and returns an exit status of 127.
- If the search was successful or if the command contains slashes, the shell executes the command in a separate execution environment.
- If execution fails because the file is not executable and not a directory, it is assumed to be a shell script.
- If the command was not begun asynchronously, the shell waits for the command to complete and collects its exit status.
When a file containing shell commands is used as the first non-option argument when invoking Bash (without -c or -s, this will create a non-interactive shell. This shell first searches for the script file in the current directory, then looks in PATH if the file cannot be found there.
1.5. Developing good scripts
1.5.1. Properties of good scripts
This guide is mainly about the last shell building block, scripts. Some general considerations before we continue:
- A script should run without errors.
- It should perform the task for which it is intended.
- Program logic is clearly defined and apparent.
- A script does not do unnecessary work.
- Scripts should be reusable.
The structure of a shell script is very flexible. Even though in Bash a lot of freedom is granted, you must ensure correct logic, flow control and efficiency so that users executing the script can do so easily and correctly.
When starting on a new script, ask yourself the following questions:
- Will I be needing any information from the user or from the user's environment?
- How will I store that information?
- Are there any files that need to be created? Where and with which permissions and ownerships?
- What commands will I use? When using the script on different systems, do all these systems have these commands in the required versions?
- Does the user need any notifications? When and why?
The table below gives an overview of programming terms that you need to be familiar with:
Table 1-1. Overview of programming terms
TermWhat is it?Command controlTesting exit status of a command in order to determine whether a portion of the program should be executed.Conditional branchLogical point in the program when a condition determines what happens next.Logic flowThe overall design of the program. Determines logical sequence of tasks so that the result is successful and controlled.LoopPart of the program that is performed zero or more times.User inputInformation provided by an external source while the program is running, can be stored and recalled when needed.
1.5.4. A word on order and logic
In order to speed up the developing process, the logical order of a program should be thought over in advance. This is your first step when developing a script.
A number of methods can be used; one of the most common is working with lists. Itemizing the list of tasks involved in a program allows you to describe each process. Individual tasks can be referenced by their item number.
Using your own spoken language to pin down the tasks to be executed by your program will help you to create an understandable form of your program. Later, you can replace the everyday language statements with shell language words and constructs.
The example below shows such a logic flow design. It describes the rotation of log files. This example shows a possible repetitive loop, controlled by the number of base log files you want to rotate:
- Do you want to rotate logs?
- If yes:
- Enter directory name containing the logs to be rotated.
- Enter base name of the log file.
- Enter number of days logs should be kept.
- Make settings permanent in user's crontab file.
- If no, go to step 3.
- Do you want to rotate another set of logs?
- If yes: repeat step 1.
- If no: go to step 3.
The user should provide information for the program to do something. Input from the user must be obtained and stored. The user should be notified that his crontab will change.