Bash (shell)

from Wikipedia, the free encyclopedia
Bash - The Bourne-again shell

Bash demo.png
Example of a bash session
Basic data

Maintainer Chet Ramey
developer Chet Ramey
Publishing year June 8, 1989
Current  version 5.0
( January 7, 2019 )
operating system various
programming language C.
category Command line interpreter
License GNU General Public License, version 3.0 or later
German speaking Yes

Bash (also BASH or bash ), the Bourne-again shell , is a free Unix shell under GPL .

As Shell Bash is a man-machine interface , the (a surrounding area English environment ) provides, in the line-wise text typed and - issues are possible. The latter is done via the command line , in which commands are typed and entered by pressing the enter key.

Bash is an integral part of the Unix-like operating system GNU and is part of the GNU project . Bash is also the default shell for most operating systems based on GNU / Linux . In addition, Bash 3.x was the default shell in macOS from Apple (10.3–10.14) from 2003 to 2019 - but was never updated to version 4.0 or higher for licensing reasons.

The name Bash is ambiguous in English ( [to] bash, [the] bash ) and has acquired more, mostly humorous meanings over time.


The bash was written in 1987 by Brian Fox for the GNU project and was paid for by the FSF . In 1990 the project was taken over by Chet Ramey . Version 3 was released on July 27, 2004. Version 4 was released on February 20, 2009 and brought some innovations with it. These include a new output redirection , associative arrays, and a new wildcard ( **).

Version 4.4 was released in September 2016.


Comparison with other shells

The shell is largely compatible with the Bourne shell (sh) and controlled addition of both most of the functions of the grain shell (ksh) and portions of the C-Shell - syntax , such as the history of previously entered commands, the $RANDOMvariable and the POSIX - Form of command substitution $(...)were adopted. Some elements such as the **wildcard have been taken over from the Z shell (zsh). It was also expanded to include functions such as B. the integer arithmetic without the execution of external processes and simplification of the I / O redirects. Based on its user-specific configuration file, Bash also offers ~/.bashrcthe option of allowing each user their own settings, such as an individual design of the prompt, across sessions.


A subshell is a shell process that was created by a shell. Programs that are executed by entering a command in a shell are started in a subshell depending on the operating system: Before the shell can start a program as a child process, it must create a shell process. When executing a program from the graphical user interface, however, no shell or subshell is involved.

Subshells are created automatically when a number of bash features are used. For example, all commands in a pipeline are executed in their own subshell. Shells in graphical interfaces, such as ktermor gnome-terminal, are not subshells because their parent process is not a shell.

Login shell

If the bash is started with a program that is used to log on to the console, such as B. login, su - , ssho. Ä., Or with the option -lcalled, it acts as an interactive login shell. The use of bash as a login shell for a user is determined by the corresponding user entry in the /etc/passwd. The behavior of a login shell differs from that of a non-login shell. As each shell reads the Bash (initially) the system-wide configuration file /etc/profileand executes the instructions contained in it, after which they examined in the user directory of the user logged in turn ~/.bash_profile, ~/.bash_login, ~/.profilewherein only the first found file is read. When the login shell is closed, the logged in user is logged out ( logoutor exit) or ^DCTRL-D (or CTRL-D on the German keyboard). When you log out - if available - the will be ~/.bash_logoutexecuted.


Bash is configured through a number of configuration files. A distinction must be made between system-wide and user-specific configuration. The system-wide settings bash are - depending on the distribution - in /etc/bash.bashrcor /etc/bashrcstored. The system-wide /etc/profileis already evaluated by the login shell. The shell profile is executed every time a shell instance starts, including (but not only) when a login shell is started.

The directory /etc/skelprovides templates for the configuration files that are copied to the home directory of a newly created user.

Built-in commands

Bash has many built-in commands and reserved words. Among the built-in instructions include, for example alias, cd, exit, help, kill, logoutor pwdand are among the reserved words, necessary to the Bash programming keywords like case, for, functionor while.


As a placeholder Bash uses *(including none) for any number of characters and ?a sign of exactly. With [ ]you can also { }specify sets of characters and with so-called bracket expansion. Examples:

user1@blablubb:~/test$ ls datei*
datei datei1 datei2 datei3 datei4 datei5 datei6  # Auch 'datei' ohne Nummer
user1@blablubb:~/test$ ls datei?
datei1 datei2 datei3 datei4 datei5 datei6
user1@blablubb:~/test$ ls datei[1-3]          # Wertebereich
datei1 datei2 datei3
user1@blablubb:~/test$ ls datei[135]          # Wertemenge
datei1 datei3 datei5
user1@blablubb:~/test$ touch test{1..5}         # Wertebereich für Klammernexpansion
user1@blablubb:~/test$ ls test*
test1 test2 test3 test4 test5

Input and output, redirection

As usual in Unix, Bash supports the redirection of the three standard data streams (including standard channels), which are handled using the so-called file descriptors . These are the standard input stdin(channel 0), the standard output stdout(channel 1) and the standard error output stderr(channel 2). A program running in bash reads from standard input, usually the keyboard, and passes the result on to standard output, usually the screen. Error messages are usually output via the standard error output stderron the standard stdoutoutput, i.e. on the screen, as the following example illustrates:

user@blablubb:~/test$ ls -l /root ~/test
insgesamt 0
-rw-r--r-- 1 user1 users 0 Feb 11 20:03 datei1
-rw-r--r-- 1 user1 users 0 Feb 11 20:03 datei2
ls: Öffnen von Verzeichnis /root nicht möglich: Keine Berechtigung

The channels can also be addressed more briefly using their numbers. The standard channels can also be rerouted by using the diversion characters <(channel 0), >(channel 1) and |(concatenation of input and output) after the respective command . In the example above, the error message was output on the screen together with the standard output. If you want to separate error and standard output and save them in different files, you can do this using the channel numbers:

user@blablubb:~/test$ ls -l /root ~/test >ls.txt 2>ls_err.txt

In the following, the standard ls *.txtoutput is verzeichnis.inforedirected to the file and then lessread in as standard input:

user@blablubb:~/test$ ls *.txt >
user@blablubb:~/test$ less <

The pipe (|) can be used to chain commands by combining the output of the first command with the input of the second command, which can be extended (almost) at will:

user@blablubb:~/test$ ls -l /etc | grep '^d' | wc -l

The mechanism of redirecting the standard channels and the pipe are not special to Bash.

set options

Bash has 27 options that can be used to set a different operating mode. All possible settings can be made with

user$ set -o

are listed, with the option -othe modes are listed or set and with the option +othe option is canceled. That +is to -be read as something crossed out . Commonly used are the options noclobberwith which the output redirection is prevented from overwriting existing files , the option noglobwith which no wildcards such as *and are ?possible for file names and the option xtracewith which each shell command is output again before it is executed, with the extensions made internally, which can be useful when troubleshooting .

Overwriting the output redirection is suppressed:

user$ echo "hallo" > hallo.txt
user$ echo "hallo" > hallo.txt
user$ set -o noclobber
user$ echo "hallo" > hallo.txt
-su: hallo.txt: Kann existierende Datei nicht überschreiben.
user$ set +o noclobber
user$ echo "hallo" > hallo.txt

You can create file names with an asterisk and question mark. ls *shThe option must be reset before the query with , otherwise the file would be *shsearched for.

user$ set -o noglob
user$ touch da*
user$ touch dat?
user$ set +o noglob
user$ ls *sh
da* dat?

The third line shows the extensions made by bash:

user$ set -o xtrace
user$ ls *.txt
+ ls --color=auto hallo.txt test.txt text.txt
hallo.txt test.txt text.txt

All options can also be set using a shorthand, for example is set -Csynonymous with set -o noclobber.

Environment variables

Variables can be defined in bash in several ways. A basic distinction is made between “local” variables, which only apply in the shell in which they were defined, and “global” variables, which are also assigned in sub- processes . Local variables can be created with:

user$ var=var1
user$ var='var zwei'
user$ let var=var3
user$ declare var=var4

and global variables are defined with:

user$ declare -x var=var5
user$ export var=var6

The variables are addressed with the $symbol:

user$ echo $var

Instead of “global variable”, however, the expression “ environment variable ” is much more common and more appropriate in the Unix environment , because in some programming languages ​​the expression “ global variable ” has a different meaning than “environment variable”. A value assignment for an "environment variable" only affects "subroutines" (child processes), while some programming languages ​​allow variable values ​​to be set for "main programs" (for example by prefixing \globalin TeX ).

Own environment variables

Bash has numerous of its own environment variables, which are also often evaluated by other commands. The variables PATHfor the search path, LANGfor setting the language or PS1for the prompt are known.

For example, the English-language help pages can be lscalled up with the command below. A sub-shell is created here, the LANGenvironment variable is overwritten and the command is then man lsexecuted.

user$ LANG=en man ls


Bash programming differs from other programming languages ​​in many ways. For example, when branching, the condition is traditionally not evaluated by the shell itself, but is passed to another program:

if [ Bedingung ] ; then
  # Falls Bedingung wahr ist, wird dies ausgeführt
  # Falls Bedingung falsch ist, wird dies ausgeführt

The two square brackets are not a delimiter, but a synonym for the builtin shell command test. The command testchecks the condition and returns a return value of 0 ( true ) or 1 ( false ), which is processed by the ifstatement. The code above is identical to the following notation:

if test Bedingung ; then
  # Falls Bedingung wahr ist, wird dies ausgeführt
  # Falls Bedingung falsch ist, wird dies ausgeführt

Nowadays, however, there is also a built-in expression that does not require an external command and is therefore no longer subject to the restrictions that the command form was subject to, and thus no longer requires quotation marks around variables:

if [[ Bedingung ]] ; then
  # Falls Bedingung wahr ist, wird dies ausgeführt
  # Falls Bedingung falsch ist, wird dies ausgeführt

Here the condition can now e.g. B. also contain operators like <, >(less than and greater than), and =~(comparison with regular expression ).

This variant is therefore generally recommended if compatibility with older versions is not required.

However, the return value (0–127, where> 0 = false ) of any program can still be processed. Not just that of the testcommand. As an example, the killcommand is used here to test whether a process with a certain number is still running or is able to receive signals:

if kill -0 1234 ; then
  # Prozess 1234 läuft
  # Prozess 1234 läuft nicht


In September 2014, a serious vulnerability under the name Shellshock became known. The long-standing loophole means that when a new shell is started, malicious code that has been inserted via an environment variable is executed without being checked. The gap has been considered closed since October 2014.


  • Christian Meißner: Bash - working and programming with the shell . Open Source Press, 2011, ISBN 978-3-941841-44-4 .
  • Karsten Günther: Bash - in a nutshell . 2008, ISBN 978-3-89721-533-7 .
  • Cameron Newham, Bill Rosenblatt: Learning the Bash Shell. 3rd ed., O'Reilly & Associates, 2009, ISBN 0-596-00965-8 .
  • Jürgen Wolf, Stefan Kania: Shell programming. The comprehensive manual . Galileo Computing, 4th edition 2013, ISBN 978-3-8362-2310-2 .

Web links

Wikibooks: Linux practical book / Shell programming  - learning and teaching materials

Individual evidence

  1. Bash-5.0 release available . January 7, 2019 (accessed January 8, 2019).
  2. The bash Open Source Project on Open Hub: Languages ​​Page . In: Open Hub . (accessed on September 3, 2018).
  3. ^ Licensing of Bash . (accessed on October 3, 2016).
  4. ^ About the GNU Project - GNU Project - Free Software Foundation (FSF). April 24, 2011, accessed May 11, 2019 .
  6. NEWS
  7. Bash 4.4. published ,, September 19, 2016.
  8. ShellShock: Standard Unix Shell Bash allows the execution of malicious code , heise online
  9. Michał Zalewski: Bash bug: the other two RCEs, or how we chipped away at the original fix (CVE-2014-6277 and '78). In: lcamtuf blog. October 1, 2014, accessed October 31, 2014 .
  10. Bash Code Injection Vulnerability via Specially Crafted Environment Variables (CVE-2014-6271, CVE-2014-7169). Red Hat , October 2, 2014; accessed November 1, 2014 .