Questions tagged [shell]

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

12081 questions
244
votes
3 answers

What's the difference between semicolon and double ampersand &&

What is the difference between echo "Hello " ; echo "world" and echo "Hello " && echo "world" Both seems to run the two commands after each other.
lindhe
  • 4,236
  • 3
  • 21
  • 35
221
votes
11 answers

How to check if a shell is login/interactive/batch

I think I understand the differences between an interactive, a login and a batch shell. See the following links for more help: What is the difference between a 'Login' and an 'Interactive' bash shell (from the sister site: Server Fault) Difference…
180
votes
4 answers

What does ampersand mean at the end of a shell script line?

sh sys-snap.sh & What is sh? What is sys-snap.sh? Why I should put & at the end of the line? Can anyone explain the syntax? Without the & the script won't go back to the prompt till I press Ctrl+C. With & I can press enter and it works.
user4951
  • 10,519
175
votes
6 answers

What's the meaning of a dot before a command in shell?

While following android eclipse debug tutorial, I encounter following commands. cd /path/to/android/root . build/envsetup.sh lunch 1 make emulator My problem is what the dot before build/envsetup.sh means?
Jichao
  • 1,977
122
votes
12 answers

How to test what shell I am using in a terminal?

How to check what shell I am using in a terminal? What is the shell I am using in MacOS?
user5837
  • 1,321
107
votes
4 answers

How to undo `set -x`?

I typed set -x in terminal. Now the terminal keeps printing the last command run on top of my output so the command ~]$echo "this is what I see" returns + echo 'this is what I see' this is what I see There is no man page for set, how do I turn…
103
votes
2 answers

Difference between 'echo' and 'echo -e'

What is the difference between echo and echo -e? And which quotes ("" or '') should be used with the echo command? i.e: echo "Print statement" or echo 'Print statement'? Also, what are the available options that can be used along with echo?
Venkatesh
  • 1,171
90
votes
4 answers

Understanding IFS

The following few threads on this site and StackOverflow were helpful for understanding how IFS works: What is IFS in context of for looping? How to loop over the lines of a file Bash, read line by line from file, with IFS But I still have some…
77
votes
4 answers

Why doesn't the tilde (~) expand inside double quotes?

According to this answer and my own understanding, the tilde expands to the home directory: $ echo ~ /home/braiam Now, whenever I want the shell expansion to work, i. e. using variable names such $FOO, and do not break due unexpected characters,…
Braiam
  • 35,991
58
votes
5 answers

What are the fundamental differences between the mainstream *NIX shells?

What are the fundamental differences between the mainstream *NIX shells and what scenarios might prompt you to use one over the other? I understand that some of it probably comes down to user preference but I've only ever used Bash and I'm…
conorgriffin
  • 1,553
48
votes
9 answers

How could I remember how to use redirection?

I know what program > /dev/null 2>&1 does. It redirects the output to /dev/null and 2>&1 means to redirect the error output in the same place where the output is sent. My problem is I always have to google it because I never remember it. So, I…
Luc M
  • 4,095
  • 5
  • 30
  • 29
48
votes
5 answers

Is there a difference between `;` and `&&` and `|`?

When you want to run multiple commands you can use ;, && and | Like this: killall Finder; killall SystemUIServer, cd ~/Desktop/ && rm Caches Or: man grep | man cat for example. But, is there a difference between |, ; and &&? If so, what is the…
DisplayName
  • 11,688
48
votes
3 answers

How do I switch from an unknown shell to bash?

I was surprised that I didn't find this question already on the site. So, today $ came up after I logged in as a new user. This was unexpected because my main user's prompt starts with username@computername:~$. So, how do I switch from this other…
mouche
  • 1,315
  • 2
  • 16
  • 15
48
votes
6 answers

To env or not to env

What is the difference between the command $ env FOO=bar baz and $ FOO=bar baz What effect does env have?
43
votes
4 answers

Why do many commands provide a "quiet" option?

Why do many commands provide the option -q or --quiet to suppress output when you can easily achieve the same thing by redirecting standard output to the null file?
1
2 3
26 27