Bash (shell)
Bash - The Bourne-again shell
|
|
---|---|
![]() |
|
![]() 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 |
www.gnu.org/software/bash/ |
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.
history
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.
Functionality
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 $RANDOM
variable 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 ~/.bashrc
the option of allowing each user their own settings, such as an individual design of the prompt, across sessions.
Subshell
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 kterm
or 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 -
, ssh
o. Ä., Or with the option -l
called, 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/profile
and executes the instructions contained in it, after which they examined in the user directory of the user logged in turn ~/.bash_profile
, ~/.bash_login
, ~/.profile
wherein only the first found file is read. When the login shell is closed, the logged in user is logged out ( logout
or exit
) or ^D
CTRL-D (or CTRL-D on the German keyboard). When you log out - if available - the will be ~/.bash_logout
executed.
configuration
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.bashrc
or /etc/bashrc
stored. The system-wide /etc/profile
is 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/skel
provides 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
, logout
or pwd
and are among the reserved words, necessary to the Bash programming keywords like case
, for
, function
or while
.
placeholder
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
user1@blablubb:~/test$
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 stderr
on the standard stdout
output, i.e. on the screen, as the following example illustrates:
user@blablubb:~/test$ ls -l /root ~/test
/home/user/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 *.txt
output is verzeichnis.info
redirected to the file and then less
read in as standard input:
user@blablubb:~/test$ ls *.txt > verzeichnis.info
user@blablubb:~/test$ less < verzeichnis.info
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 -o
the modes are listed or set and with the option +o
the option is canceled. That +
is to -
be read as something crossed out . Commonly used are the options noclobber
with which the output redirection is prevented from overwriting existing files , the option noglob
with which no wildcards such as *
and are ?
possible for file names and the option xtrace
with 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 *sh
The option must be reset before the query with , otherwise the file would be *sh
searched for.
user$ set -o noglob
user$ touch da*tei.sh
user$ touch dat?ei.sh
user$ set +o noglob
user$ ls *sh
da*tei.sh dat?ei.sh
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 -C
synonymous 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 \global
in TeX ).
Own environment variables
Bash has numerous of its own environment variables, which are also often evaluated by other commands. The variables PATH
for the search path, LANG
for setting the language or PS1
for the prompt are known.
For example, the English-language help pages can be ls
called up with the command below. A sub-shell is created here, the LANG
environment variable is overwritten and the command is then man ls
executed.
user$ LANG=en man ls
programming
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
else
# Falls Bedingung falsch ist, wird dies ausgeführt
fi
The two square brackets are not a delimiter, but a synonym for the builtin shell command test
. The command test
checks the condition and returns a return value of 0 ( true ) or 1 ( false ), which is processed by the if
statement. The code above is identical to the following notation:
if test Bedingung ; then
# Falls Bedingung wahr ist, wird dies ausgeführt
else
# Falls Bedingung falsch ist, wird dies ausgeführt
fi
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
else
# Falls Bedingung falsch ist, wird dies ausgeführt
fi
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 test
command. As an example, the kill
command 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
else
# Prozess 1234 läuft nicht
fi
safety
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.
literature
- 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
- Homepage
- Bash on the GNU side
- SelfLinux: Shell programming
- Advanced Bash Scripting Guide (English)
- Oh my bash
Individual evidence
- ↑ Bash-5.0 release available . January 7, 2019 (accessed January 8, 2019).
- ↑ The bash Open Source Project on Open Hub: Languages Page . In: Open Hub . (accessed on September 3, 2018).
- ^ Licensing of Bash . (accessed on October 3, 2016).
- ^ About the GNU Project - GNU Project - Free Software Foundation (FSF). April 24, 2011, accessed May 11, 2019 .
- ↑ heise.de
- ↑ NEWS
- ↑ Bash 4.4. published , linux-magazin.de, September 19, 2016.
- ↑ ShellShock: Standard Unix Shell Bash allows the execution of malicious code , heise online
- ↑ 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 .
- ↑ Bash Code Injection Vulnerability via Specially Crafted Environment Variables (CVE-2014-6271, CVE-2014-7169). Red Hat , October 2, 2014; accessed November 1, 2014 .