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

11478 questions
1508
votes
10 answers

What is the exact difference between a 'terminal', a 'shell', a 'tty' and a 'console'?

I think these terms almost refer to the same thing, when used loosely: terminal shell tty console What exactly does each of these terms refer to?
Lazer
  • 33,497
  • 24
  • 68
  • 74
782
votes
13 answers

How do I grep for multiple patterns with pattern having a pipe character?

I want to find all lines in several files that match one of two patterns. I tried to find the patterns I'm looking for by typing grep (foo|bar) *.txt but the shell interprets the | as a pipe and complains when bar isn't an executable. How can I…
Dan
  • 9,122
  • 5
  • 24
  • 38
737
votes
4 answers

Difference between nohup, disown and &

What are the differences between $ nohup foo and $ foo & and $ foo & $ disown
Lesmana
  • 25,703
  • 20
  • 76
  • 82
734
votes
5 answers

What does "--" (double-dash) mean?

I have seen -- used in the compgen command. For example: compgen -W "foo bar baz" -- b What is the meaning of the -- in there?
dogbane
  • 27,629
  • 13
  • 78
  • 60
551
votes
10 answers

How to append multiple lines to a file

I am writing a bash script to look for a file if it doesn't exist then create it and append this to it: Host localhost ForwardAgent yes So "line then new line 'tab' then text" I think its a sensitive format. I know you can do this: cat…
TheLegend
  • 5,955
  • 4
  • 14
  • 14
507
votes
7 answers

Difference between Login Shell and Non-Login Shell?

I understand the basic difference between an interactive shell and a non-interactive shell. But what exactly differentiates a login shell from a non-login shell? Can you give examples for uses of a non-login interactive shell?
Igorio
  • 6,779
  • 7
  • 19
  • 11
499
votes
6 answers

What is the difference between the Bash operators [[ vs [ vs ( vs ((?

I am a little bit confused on what do these operators do differently when used in bash (brackets, double brackets, parenthesis and double parenthesis). [[ , [ , ( , (( I have seen people use them on if statements like this : if [[condition]] if…
RetroCode
  • 5,139
  • 3
  • 9
  • 7
498
votes
14 answers

Turn off buffering in pipe

I have a script which calls two commands: long_running_command | print_progress The long_running_command prints progress but I'm unhappy with it. I'm using print_progress to make it nicer (namely, I print the progress in a single line). The…
Aaron Digulla
  • 5,668
  • 4
  • 18
  • 17
463
votes
10 answers

How do I delete a file whose name begins with "-" (hyphen a.k.a. dash or minus)?

How do you remove a file whose filename begins with a dash (hyphen or minus) -? I'm ssh'd into a remote OSX server and I have this file in my directory: tohru:~ $ ls -l total 8 -rw-r--r-- 1 me staff 1352 Aug 18 14:33 --help ... How in the…
Astra
  • 4,733
  • 2
  • 15
  • 6
459
votes
5 answers

Why not use "which"? What to use then?

When looking for the path to an executable or checking what would happen if you enter a command name in a Unix shell, there's a plethora of different utilities (which, type, command, whence, where, whereis, whatis, hash, etc). We often hear that…
Stéphane Chazelas
  • 481,585
  • 88
  • 947
  • 1,398
457
votes
17 answers

Get exit status of process that's piped to another

I have two processes foo and bar, connected with a pipe: $ foo | bar bar always exits 0; I'm interested in the exit code of foo. Is there any way to get at it?
Michael Mrozek
  • 87,955
  • 34
  • 234
  • 230
440
votes
10 answers

How do I use pushd and popd commands?

What are the practical uses of both pushd and popd when there is an advantage of using these two commands over cd and cd -? EDIT: I'm looking for some practical examples of uses for both of these commands or reasons for keeping stack with…
syntagma
  • 11,441
  • 21
  • 56
  • 70
398
votes
3 answers

What are the shell's control and redirection operators?

I often see tutorials online that connect various commands with different symbols. For example: command1 | command2 command1 & command2 command1 || command2 command1 && command2 Others seem to be connecting commands to files: command1 >…
terdon
  • 220,769
  • 58
  • 415
  • 622
396
votes
17 answers

How to do integer & float calculations, in bash or other languages/frameworks?

Using echo "20+5" literally produces the text "20+5". What command can I use to get the numeric sum, 25 in this case? Also, what's the easiest way to do it just using bash for floating point? For example, echo $((3224/3807.0)) prints 0 :(. I am…
Michael Durrant
  • 38,917
  • 68
  • 153
  • 227
392
votes
3 answers

How to check OS and version using a Linux command

What is the Linux command to check the server OS and its version? I am connected to the server using shell.
sarath
  • 4,031
  • 3
  • 12
  • 4
1
2 3
99 100