On any Unix-like system, a program is run when a user logs in on the
system. This program is called a shell, and it is used to run other
programs and perform routine tasks. The standard Unix shell is called the
Bourne shell (or sometimes the POSIX shell), and it is located at
/bin/sh
. Other popular shells include the Korn shell (usually
/bin/ksh
), and the Bourne Again shell (bash), the GNU
implementation of the POSIX shell. All of these are mostly compatible, and
this document should cover a common subset of all of the above, although on
Linux /bin/sh
is almost always bash.
(There are other shells that are not compatible, such as the C shell (csh), which is popular on BSD systems; tcsh, a free implementation of csh; and zsh, another free shell. The C shell (csh or tcsh) is not particularly well-suited for scripting. See Tom Christiansen's Csh Programming Considered Harmful for details. Other shells have syntax that may or may not resemble sh, but are probably not widespread enough to worry about.)
What makes the Unix shells so powerful is the simple fact that you are at all times in a programming environment. This is much like the BASIC environments on old computers, although the shell interpreter is much more capable.
This means that you have exactly the same commands, variables, etc. that you would when running a shell script from a file. In fact, the shell interpreter parses command-line input the same way it would parse input from a file.
Another thing that makes Unix shells so powerful is that there are very few
built-in commands, oddly enough. Anything that isn't recognized as an
built-in is run from the search path, which usually includes directories
like /bin
and /usr/bin
. This means that the
shell can be extended nearly infinitely.
Following are some basic syntax elements in sh:
;
(semicolon) or newline
&&
(double ampersand)
||
(double pipe)
&
(ampersand)
( command )
(parentheses)
$(( expression ))
echo $(( 2+1 ))
would output
3
).
\
, ''
, and ""
|
, >
, and <
` command `
or $( command )