On Unix-like operating systems, sh is the command name of the Bourne shell, the standard command language interpreter of Unix and many Unix-like operating systems, including Linux.

Syntax

sh [-acefhikmnprstuvx] [arg] …

Description and history

sh is a command language interpreter that executes commands read from a command line string, the standard input, or a specified file.

  • Syntax
  • Description and history
  • Commands
  • Command substitution
  • Parameter substitution
  • Blank interpretation
  • File name generation
  • Quoting
  • Prompting
  • Input and output
  • Environment
  • Signals
  • Execution
  • Special commands
  • Invocation
  • Job control
  • Notes
  • Files
  • Examples
  • Related commands
  • Linux commands help

The Bourne shell was developed in 1977 by Stephen Bourne at AT&T’s Bell Labs. It was the default shell of Unix Version 7. Most Unix-like systems contain the file /bin/sh that is either the Bourne shell, or a symbolic link (or hard link) to a compatible shell.

The Bourne Shell was originally developed as a replacement for the Thompson shell, whose executable file was also named sh. Although it is used as an interactive command interpreter, its original purpose was to function as a scripting language.

Features of the Bourne Shell include:

  • Scripts can be invoked as commands using their file name.
  • The shell may be used interactively or non-interactively.
  • Commands may be executed synchronously or asynchronously.
  • The shell supports input and output redirection, and pipelines.
  • A robust set of built-in commands.
  • Flow control constructs, quotation facilities, and functions.
  • Typeless variables.
  • Both local and global variable scopes.
  • Scripts can be interpreted, i.e., they do not have to be compiled to be executed.
  • Command substitution using back quotes, e.g.: command.
  • “Here documents”: the use of « to embed a block of input text within a script.
  • “for/do/done” loops, in particular the use of $* to loop over arguments.
  • “case/in/esac” selection mechanism, primarily intended to assist argument parsing.
  • Support for environment variables using keyword parameters and exportable variables.
  • Strong provisions for controlling input and output and in its expression-matching facilities.

Use of the Bourne Shell has largely been superceded by the Bourne-Again Shell (bash), which supports more user-friendly interactive features such as job control and a command history.

Commands

A simple-command is a sequence of non-blank words separated by blanks (a blank is a tab or a space). The first word specifies the name of the command to be executed. Except as specified below, the remaining words are passed as arguments to the invoked command. The command name is passed as argument 0 (see exec). The value of a simple-command is its exit status if it terminates normally or 200+status if it terminates abnormally (see our operating system signals overview for a list of status values).

A pipeline is a sequence of one or more commands separated by a vertical bar ("|"). The standard output of each command but the last connects by a pipe to the standard input of the next command. Each command is run as a separate process; the shell waits for the last command to terminate. The value of a pipeline is the exit status of its last command.

A list is a sequence of one or more pipelines separated by “;”, “&”, “&&” or “||” and optionally terminated by “;” or “&”. “;” and “&” have equal precedence that is lower than that of “&&” and “||”, “&&” and “||” also have equal precedence. A semicolon causes sequential execution; an ampersand causes the preceding pipeline to be executed without waiting for it to finish. The symbol “&&” ("||") causes the list following to be executed only if the preceding pipeline returns a zero (non zero) value. Newlines may appear in a list, instead of semicolons, to delimit commands.

A “#” at the beginning of a word starts a comment and causes the rest of the line to be ignored.

A command is either a simple-command or one of the following. The value returned by a command is that of the last simple-command executed in the command:

The following words are only recognized as the first word of a command, and when not enclosed in quotes:

  • if
  • then
  • else
  • elif
  • fi
  • case
  • in
  • esac
  • for
  • while
  • until
  • do
  • done
  • {
  • }

Command substitution

The standard output from a command enclosed in a pair of grave accents (``) may be used as part or all of a word; trailing newlines are removed. For example, if the executable script echotest.sh contained the command:

echo “The name of this script is basename $0.”

Then running the script would display the combined output of echo and basename:

The name of this script is echotest.sh.

Parameter substitution

The character $ is used to introduce substitutable parameters. Positional parameters may be assigned values by set. Variables may be set in the form “name=value [ name=value ] …”.

If the : is omitted, the substitutions are only executed if the parameter is set, even if it’s empty.

In the above, word is not evaluated unless it is to be used as the substituted string. So, for example, “echo ${d-pwd}” only executes pwd if d is unset.

The following parameters are automatically set by the shell:

The following parameters are used by the shell:

Blank interpretation

After parameter and command substitution, any results of substitution are scanned for internal field separator characters (those found in $IFS) and split into distinct arguments where such characters are found. Explicit null arguments ("" or ‘’) are retained. Implicit null arguments (those resulting from parameters that have no values) are removed.

File name generation

Following substitution, each command word is scanned for the characters “*”, “?” and “[”. If one of these characters appears then the word is regarded as a pattern. The word is replaced with alphabetically sorted file names that match the pattern. If no file name is found that matches the pattern then the word is left unchanged. The character . at the start of a file name or immediately following a “/”, and the character “/”, must be matched explicitly.

Quoting

The following characters have a special meaning to the shell and cause termination of a word unless quoted:

  • ;
  • &
  • (
  • )
  • |
  • ^
  • <
  • newline
  • space
  • tab

A character may be quoted by preceding it with a “". “\newline” is ignored. All characters enclosed between a pair of quote marks (’’), except a single quote, are quoted. Inside double quotes (”") parameter and command substitution occurs and “" quotes the characters , ` " and $.

‘$*’ is equivalent to ‘$1 $2 …’, whereas ‘[email protected]’ is equivalent to ‘"$1” “$2”…’.

Prompting

When used interactively, the shell prompts with the value of $PS1 before reading a command. If at any time a newline is typed and further input is needed to complete a command then the secondary prompt ($PS2) is issued.

Input and output

Before a command is executed its input and output may be redirected using a special notation interpreted by the shell. The following may appear anywhere in a simple-command or may precede or follow a command and are not passed on to the invoked command. Substitution occurs before word or digit is used:

If one of the above is preceded by a digit then the file descriptor created is that specified by the digit (instead of the default 0 or 1). For example, “… 2>&1” creates file descriptor 2 to be a duplicate of file descriptor 1. If a command is followed by & then the default standard input for the command is the empty file (/dev/null), unless job control is enabled. Otherwise, the environment for the execution of a command contains the file descriptors of the invoking shell as modified by input/output specifications.

Environment

The environment is a list of name-value pairs that is passed to an executed program in the same way as a normal argument list; see exec and environ. The shell interacts with the environment in several ways. On invocation, the shell scans the environment and creates a parameter for each name found, giving it the corresponding value. Executed commands inherit the same environment. If the user modifies the values of these parameters or creates new ones, none of these affects the environment unless the export command is used to bind the shell’s parameter to the environment. The environment seen by any executed command is thus composed of any unmodified name-value pairs originally inherited by the shell, plus any modifications or additions, all of which must be noted in export commands.

The environment for any simple-command may be augmented by prefixing it with one or more assignments to parameters. Thus these two lines are equivalent:

TERM=450 cmd args

(export TERM; TERM=450; cmd args)

Signals

The INTERRUPT and QUIT signals for an invoked command are ignored if the command is followed by & (unless job control is enabled); otherwise signals have the values inherited by the shell from its parent. See also trap.

Execution

Each time a command is executed the above substitutions are carried out. The shell then first looks if a function with the command name was defined; if so, it is chosen for execution. Otherwise, except for the ‘special commands’ listed below a new process is created and an attempt is made to execute the command via an exec.

The shell parameter $PATH defines the search path for the directory containing the command. Each alternative directory name is separated by a colon (":"). The default path is ‘/usr/sbin:/bin:/usr/bin:’. If the command name contains a / then the search path is not used. Otherwise, each directory in the path is searched for an executable file. If the file has execute permission but is not an a.out file, it is assumed to be a file containing shell commands. A subshell (i.e., a separate process) is spawned to read it. A parenthesized command is also executed in a subshell.

Special commands

Invocation

If the first character of argument zero is -, commands are read from /etc/profile and $HOME/.profile, if the respective file exists. Commands are then read as described below. The following flags are interpreted by the shell when it is invoked:

cd /usf/lb

cd /usr/lib? yok

The remaining flags and arguments are described under the set command.

Job control

When an interactive shell is invoked as jsh, job control is enabled. Job control allows to stop and resume processes, and to switch between foreground and background jobs. A job consists of the commands of a single pipeline. Each job is placed in a separate process group; a login shell and all jobs created by it form a session. Interrupt, quit, and other terminal control characters only affect the current foreground process group. The foreground job can be stopped pressing the suspend key, often ^Z; any job can be stopped by sending the STOP signal to it. Jobs are identified by job IDs of the following form:

The following built-in commands are additionally available with job control:

Notes

For historical reasons, ^ is a synonym for | as pipeline separator. Its use in new applications is therefore discouraged.

If a command other than a simple-command (i.e., ‘for …’, ‘case …’, etc.) is redirected, it is executed in a subshell. If variable assignments must be visible in the parent shell after the input is redirected, the exec special command can be used:

exec 5<&0 <input while read line do … variable=value … done exec <&5 5<&-

If parameters that were inherited from the initial environment are modified, they must be explicitly exported to make the change visible to external commands, as described under ‘Environment’ above.

The $IFS parameter is applied to any unquoted word. Thus:

IFS=X echoXfoo

executes the ’echo’ command with the argument ‘foo’. The command ‘set –’ without further arguments is a no-op (no operation). The shift special command can delete all positional parameters.

There is only one namespace for both functions and parameters. A function definition deletes a parameter with the same name and vice-versa.

Parameter assignments that precede a special command affect the shell itself; parameter assignments that precede the call of a function are ignored.

Files

/etc/profile $HOME/.profile /tmp/sh* /dev/null

Examples

sh

Invokes the Bourne shell, and places you at a command prompt.

bc — A calculator.init — The parent of all processes on the system.kill — Send a signal to a process, affecting its behavior or killing it.ksh — The Korn shell command interpreter.login — Begin a session on a system.newgrp — Log into a new group.ps — Report the status of a process or processes.pwd — Print the name of the working directory.stty — Set options for your terminal display.