The shell is Unix's command-line interface. You can type commands in a shell interactively, or write scripts to automate tasks. Use this tag for questions applying to /bin/sh and most compatible shells (ash, bash, ksh, zsh, …). For shell scripts with errors, please check them in http://shellcheck.net before posting here.

The shell is a program whose primary role is to enable the user to call other programs and make them interact. In a Unix context, a shell is almost always a command-line interpreter.

More precisely, unless otherwise specified, a Unix shell is compatible with the POSIX / The Open Group Base Specifications shell specification. Most Unices have such a shell available as /bin/sh.

Use this tag for questions that apply to most Bourne/POSIX-style shells. For questions about a specific shell, use that shell's tag instead (see below).

Related tags

  • for when you want to do something without a GUI, and your question isn't particularly about the command interpreter
  • for questions about automating a task, when you don't specifically need the automation to be a shell script
  • for questions about the surrounding text mode environment

Shell implementations

Main Bourne-style shells

  • The Bourne shell is one of the two surviving shells from the old days, now mostly superseded by various shells called ash, ksh and bash. The POSIX specification builds on the Bourne shell.
  • Bash is a Bourne-style, POSIX-compliant shell from the GNU project. It is the default interactive and scripting shell on most Linux distributions, and available on most other Unices. Bash adds many features, both for scripting and for interactive use.
  • Ksh is a Bourne-style, POSIX-compliant shell. It adds many advanced features, mostly for scripting. Although ksh has been open-source since 2000, it is still less favored in the open source world, and there are several partial ksh clones, like .
  • Zsh is mostly Bourne-style, but with a few syntactic differences. It has a POSIX emulation mode. It has many extra features, both for scripting and for interactive use.
  • Almquish Shell is a relatively small bourne-compatible shell. It has two famous forks:
    • Busybox contains a mostly POSIX-compliant shell (busybox ash) with some line edition capabilities together with many simple utilities. It is targeted towards embedded systems, and is therefore optimized aggressively for size. Busybox also contains another small bourne-like shell called hush.
    • Dash is a POSIX-compliant implementation of /bin/sh that aims to be as small as possible. It does this without sacrificing speed where possible.

Other well-known shells

  • The C shell is one of the two surviving shells from the old days. It is not favored for scripting. The main implementation today is tcsh. C shells used to have more interactive features than Bourne-style shells, but bash and zsh have now overtaken tcsh.
  • Fish is a relative newcomer inspired by classical and aiming to combine power and simplicity.

Further reading

Interactive use

These are features commonly found in shells with good interaction support (bash, tcsh, zsh, fish):

  • command line edition, often with configurable key bindings. in bash.
  • a history of commands that can be navigated with the Up and Down keys, searched, etc.; also a recall mechanism based on expanding sequences beginning with !.
  • completion of partially-entered file names, command names, options and other arguments.
  • management of background processes.
  • showing a prompt before each command, which many users like to configure.
  • defining short names for often-used commands

Further reading

Shell scripting

Shells have traditional control structures (conditionals, loops) as well as means to combine processes (in particular the pipe). They have built-in support for a few tasks such as arithmetic and basic string manipulation, but rely on external commands for other things.

Almost every unix-like system provides a POSIX-compliant shell, usually as /bin/sh. So scripts aiming to be portable between Unix variants should be written according to that standard, and start with the #!/bin/sh shebang line.

Many systems have at least ksh or bash available. These provide a number of useful extensions, though not always with the same syntax. Features present in both (and in zsh) include local variables in function, array variables, the double bracket syntax for conditionals ([[ … ]]), and (requiring an option to be set in bash and zsh) additional globbing patterns such as @(…).

A common difficulty in shell programming is quoting. Unlike in most programming languages, almost everything is a string, and quoting is only necessary around special characters. However some cases are tricky. In particular, a common pitfall is that variable and common substitions ($foo, $(foo)) undergo further expansion and should be protected by double quotes ("$foo", "$(foo)") unless that further expansion is desired.

Useful tools

  • checkbashisms (from the devscripts package) is a useful tool for ensuring that a shell script is POSIX-compliant and does not use features not specified by POSIX.

  • ShellCheck is web application that analyses shell scripts and checks for common mistakes (both syntax and semantic errors) as well as compliance with the POSIX standard, e.g., it highlights unquoted variables and Bashisms (if sh is used in the shebang). It can also be installed as a command-line tool which can be run locally.

Related tags

  • a tricky aspect of shell programming
  • globbing, i.e., using patterns to match multiple files
  • connecting the input or output of a command to a file
  • connecting the output of a command to the input of another command
  • two common shell tasks
  • Shells often call external utilities dedicated to one particular task.

Further reading

Code Language (used for syntax highlighting): lang-bash