https://en.wikipedia.org/wiki/Bash_%28Unix_shell%29#cite_note-25
Bash (Unix shell)
Bash is a Unix shell written by Brian Fox for the GNU Project as a free software replacement for the Bourne shell (sh).
[3]
[4] Released in 1989,
[5] it has been distributed widely as the shell for the GNU operating system and as the default shell on Linux and
Mac OS X. It has been ported to Microsoft Windows and distributed with Cygwin and MinGW, to DOS by the DJGPP project, to Novell NetWare and to Android via GNU Bash Installer
[6]
[7] and various terminal emulation applications.
Bash is a command processor, typically run in a text window, allowing the user to type commands which cause actions. Bash can also read commands from a file, called a script. Like all Unix shells, it supports filename wildcarding, piping, here documents, command substitution, variables and control structures for condition-testing and iteration.
[8] The keywords, syntax and other basic features of the language were all copied from sh. Other features, e.g., history, were copied from csh and ksh. Bash is a POSIX shell but with a number of extensions.
The name itself is an acronym, a pun, and a description. As an acronym, it stands for
Bourne-again shell, referring to its objective as a free replacement for the Bourne shell.
[9] As a pun, it expressed that objective in a phrase that sounds similar to
born again, a term for spiritual rebirth.
[10]
[11] The name is also descriptive of what it did,
bashing together the features of sh, csh and ksh.
[12]
Contents
|
History
Brian Fox began coding Bash on January 10, 1988,
[13] after Richard Stallman became dissatisfied with the lack of progress being made by a prior developer.
[3] Stallman and the Free Software Foundation (FSF) considered a free shell that could run existing sh scripts so strategic to a completely free system built from BSD and GNU code that this was one of the few projects they funded themselves, with Fox undertaking the work as an employee of FSF.
[3]
[14] Fox released Bash as a beta, version .99, on June 7, 1989
[5] and remained the primary maintainer until sometime between mid-1992
[15] and mid-1994,
[16] when he was laid off from FSF
[17] and his responsibility was transitioned to another early contributor, Chet Ramey.
[18]
[19]
[20]
Features
The Bash command syntax is a superset of the Bourne shell command syntax. The vast majority of Bourne shell scripts can be executed by Bash without modification, with the exception of Bourne shell scripts stumbling into fringe syntax behavior interpreted differently in Bash or attempting to run a system command matching a newer Bash builtin, etc. Bash command syntax includes ideas drawn from the Korn shell (ksh) and the C shell (csh) such as command line editing, command history, the directory stack, the $RANDOM and $PPID variables, and POSIX command substitution syntax $(…). When used as an interactive command shell and pressing the tab key, Bash automatically uses command line completion to match partly typed program names, filenames and variable names. The Bash command-line completion system is very flexible and customizable, and is often packaged with functions that complete arguments and filenames for specific programs and tasks.
Bash's syntax has many extensions which the Bourne shell lacks. Bash can perform integer calculations without spawning external processes, unlike the Bourne shell. Bash uses the ((…)) command and the $((…)) variable syntax for this purpose. Bash syntax simplifies I/O redirection in ways that are not possible in the traditional Bourne shell. For example, Bash can redirect standard output (stdout) and standard error (stderr) at the same time using the &> operator. This is simpler to type than the Bourne shell equivalent ' command > file 2>&1'. Bash supports process substitution using the <(command) syntax, which substitutes the output of (or input to) a command where a filename is normally used.
When using the 'function' keyword, Bash function declarations are not compatible with Bourne/Korn/POSIX scripts (the Korn shell has the same problem when using 'function'), but Bash accepts the same function declaration syntax as the Bourne and Korn shells, and is POSIX conformant. Due to these and other differences, Bash shell scripts are rarely runnable under the Bourne or Korn shell interpreters unless deliberately written with that compatibility in mind, which is becoming less common as Linux becomes more widespread. But in POSIX mode,
[21] Bash conformance with POSIX is nearly perfect.
Bash supports here documents just as the Bourne shell always has. In addition, since version 2.05b Bash can redirect standard input (stdin) from a "here string" using the <<< operator.
Bash 3.0 supports in-process regular expression matching using a syntax reminiscent of Perl.
[22]
Bash 4.0 introduced support for associative arrays.
[21]
[23] Associative arrays allow a fake support for multi-dimensional (indexed) arrays, in a similar way to AWK:
declare -A a # declare an associative array 'a' faking a bi-dimensional indexed array i=1; j=2 # initialize some indices a[$i,$j]=5 # associate value "5" to key "$i,$j" (i.e. "1,2") echo ${a[$i,$j]} # print the stored value at key "$i,$j"
Brace expansion
Brace expansion, also called alternation, is a feature copied from the C shell that generates a set of alternative combinations. The generated results need not exist as files. The results of each expanded string are not sorted and left to right order is preserved:
echo a{p,c,d,b}e # ape ace ade abe echo {a,b,c}{d,e,f} # ad ae af bd be bf cd ce cf
Brace expansions should not be used in portable shell scripts, because the Bourne shell will not produce the same output.
#!/bin/sh # A traditional shell does not produce the same output echo a{p,c,d,b}e # a{p,c,d,b}e
When brace expansion is combined with wildcards, the braces are expanded first, and then the resulting wildcards are substituted normally. Hence, a listing of JPEG and PNG images in the current directory could be obtained with:
ls *.{jpg,jpeg,png} # expands to *.jpg *.jpeg *.png - after which, # the wildcards are processed
Startup scripts
When Bash starts, it executes the commands in a variety of different scripts.
When started as an interactive login shell:
- Bash reads and executes the /etc/profile (if it exists).
- (Not infrequently, /etc/profile calls /etc/bash.bashrc.)
- After reading that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile in that order, and reads and executes the first one (that exists and is readable).
When a login shell exits:
- Bash reads and executes ~/.bash_logout (if it exists).
When started as an interactive shell (but not a login shell):
- Bash reads and executes ~/.bashrc (if it exists). This may be inhibited by using the --norc option. The --rcfile file option will force Bash to read and execute commands from file instead of ~/.bashrc.
Portability
Invoking Bash with the
--posix
option or stating
set -o posix
in a script causes Bash to conform very closely to the POSIX 1003.2 standard.
[24] Bash shell scripts intended for portability should at least take into account the Bourne shell it intends to replace. Bash has certain features that the traditional Bourne shell lacks. Among these are:
[24]
- Certain extended invocation options
- Command substitution using $( ) notation (this feature is part of the POSIX 1003.2 standard though)
- Brace expansion
- Certain array operations, and associative arrays
- The double brackets extended test construct
- The double-parentheses arithmetic-evaluation construct
- Certain string manipulation operations
- Process substitution
- A Regular Expression matching operator
- Bash-specific builtins
- Coprocesses
Keyboard shortcuts
The following shortcuts work when using default ( Emacs) key bindings. Vi-bindings can be enabled by running
set -o vi
.
[25]
Note: For shortcuts involving Alt, you may be able to use Esc instead.
Note: Sometimes, you must use Esc instead of Alt, because the Alt shortcut conflicts with another shortcut. For example, in Trisquel 5.0 (a distribution of Linux), pressing Alt+ f will not move the cursor forward one word, but will activate "File" in the menu of the terminal window.
See also: Readline
- Tab ↹ : Autocompletes from the cursor position.
- Ctrl+a : Moves the cursor to the line start (equivalent to the key Home).
- Ctrl+b : Moves the cursor back one character (equivalent to the key ←).
- Ctrl+c : Sends the signal SIGINT to the current task, which aborts and closes it.
- Ctrl+d
- Sends an EOF marker, which (unless disabled by an option) closes the current shell (equivalent to the command
exit
). (Only if there is no text on the current line) - If there is text on the current line, deletes the current character (then equivalent to the key Delete).
- Sends an EOF marker, which (unless disabled by an option) closes the current shell (equivalent to the command
- Ctrl+e : (end) moves the cursor to the line end (equivalent to the key End).
- Ctrl+f : Moves the cursor forward one character (equivalent to the key →).
- Ctrl+g : Abort the research and restore the original line.
- Ctrl+h : Deletes the previous character (same as backspace).
- Ctrl+i : Equivalent to the tab key.
- Ctrl+j : Equivalent to the enter key.
- Ctrl+k : Clears the line content after the cursor and copies it into the clipboard.
- Ctrl+l : Clears the screen content (equivalent to the command
clear
). - Ctrl+n : (next) recalls the next command (equivalent to the key ↓).
- Ctrl+o : Executes the found command from history, and fetch the next line relative to the current line from the history for editing.
- Ctrl+p : (previous) recalls the prior command (equivalent to the key ↑).
- Ctrl+r : (research) recalls the last command including the specified character(s). A second Ctrl+r recalls the next anterior command which corresponds to the research
- Ctrl+s : Go back to the next more recent command of the research (beware to not execute it from a terminal because this command also launches its XOFF). If you changed that XOFF setting, use Ctrl+q to return.
- Ctrl+t : Transpose the previous two characters.
- Ctrl+u : Clears the line content before the cursor and copies it into the clipboard.
- Ctrl+v : If the next input is also a control sequence, type it literally (e. g. * Ctrl+v Ctrl+h types "^H", a literal backspace.)
- Ctrl+w : Clears the word before the cursor and copies it into the clipboard.
- Ctrl+x Ctrl+e : Edits the current line in the $EDITOR program, or vi if undefined.
- Ctrl+x Ctrl+r : Read in the contents of the inputrc file, and incorporate any bindings or variable assignments found there.
- Ctrl+x Ctrl+u : Incremental undo, separately remembered for each line.
- Ctrl+x Ctrl+v : Display version information about the current instance of bash.
- Ctrl+x Ctrl+x : Alternates the cursor with its old position. (C-x, because x has a crossing shape).
- Ctrl+y : (yank) adds the clipboard content from the cursor position.
- Ctrl+z : Sends the signal SIGTSTP to the current task, which suspends it. To execute it in background one can enter
bg
. To bring it back from background or suspensionfg ['process name or job id']
(foreground) can be issued. - Ctrl+_ : Incremental undo, separately remembered for each line.
- Alt+b : (backward) moves the cursor backward one word.
- Alt+c : Capitalizes the character under the cursor and moves to the end of the word.
- Alt+d : Cuts the word after the cursor.
- Alt+f : (forward) moves the cursor forward one word.
- Alt+l : Lowers the case of every character from the cursor's position to the end of the current word.
- Alt+r : Cancels the changes and puts back the line as it was in the history.
- Alt+u : Capitalizes every character from the cursor's position to the end of the current word.
- Alt+. : Insert the last argument to the previous command (the last word of the previous history entry).
See also
- Comparison of command shells
References
- ^ Ramey, Chet (2011-02-16). "Bash-4.2 available for FTP". info-gnu mailing list. Retrieved 2011-02-20.
- ^ GNU Project. "README file". "Bash is free software, distributed under the terms of the [GNU] General Public License as published by the Free Software Foundation, version 3 of the License (or any later version)."
- ^ a b c Richard Stallman (forwarded with comments by Chet Ramey) (February 10, 1988). "GNU + BSD = ?". comp.unix.questions. (Web link). "For a year and a half, the GNU shell was "just about done". The author made repeated promises to deliver what he had done, and never kept them. Finally I could no longer believe he would ever deliver anything. So Foundation staff member Brian Fox is now implementing an imitation of the Bourne shell.". Retrieved Mar 22, 2011.
- ^ [|Hamilton, Naomi] (May 30, 2008), "The A-Z of Programming Languages: BASH/Bourne-Again Shell", Computerworld: 2, retrieved Mar 21, 2011, "When Richard Stallman decided to create a full replacement for the then-encumbered Unix systems, he knew that he would eventually have to have replacements for all of the common utilities, especially the standard shell, and those replacements would have to have acceptable licensing."
- ^ a b Brian Fox (forwarded by Leonard H. Tower Jr.) (June 8, 1989). "Bash is in beta release!". gnu.announce. Web link. Retrieved Oct 28 2010.
- ^ Robert Nediyakalaparambil (Dec 25, 2012). "apps". bitcubate.com. Retrieved Jan 8, 2013. "bitcubate GNU bash 4.1 installer ― just released in play store…"
- ^ Robert Nediyakalaparambil (Dec 25, 2012). "GNU bash 4.2 Installer". bitcubate.com. Retrieved Jan 8, 2013. "This app installs GNU bash 4.2 to your device. The installer works only on rooted android devices with su binary and Superuser installed."
- ^ Bourne, S.R. (July/August 1978). "The UNIX Shell". The Bell System Technical Journal (Short Hills, NJ: American Telephone and Telegraph Company) 57 (6): 1971�C1990. ISSN 00005-8580. "The lines between
<<!
and!
are called a here document; they are read by the shell and made available as the standard input." - ^ C Programming by Al Stevens, Dr. Dobb's Journal, July 1, 2001
- ^ Richard Stallman (Nov 12, 2010). "About the GNU Project". Free Software Foundation. Archived from the original on 24 April 2011. Retrieved Mar 13, 2011. "“Bourne Again Shell” is a play on the name “Bourne Shell”, which was the usual shell on Unix."
- ^ Gattol, Markus (Mar 13, 2011), Bourne-again Shell, retrieved Mar 13, 2011, "The name is a pun on the name of the Bourne shell (sh), an early and important Unix shell written by Stephen Bourne and distributed with Version 7 Unix circa 1978, and the concept of being "born again"."
- ^ Ian Darwin (June 13, 1989). "at&t-free ksh (was: job control is a bug, not a feature)". comp.os.minix. Web link. "Yup, the gnu project's Born Again Shell ("bash") is an attempt at bashing all the features of sh together with many of those from both csh and ksh.". Retrieved Mar 21, 2011.
- ^ Brian Fox (August 29, 1996), shell.c, Free Software Foundation, "Birthdate: Sunday, January 10th, 1988. Initial author: Brian Fox"
- ^ Richard Stallman (October 3, 2010). "About the GNU Project". Free Software Foundation. Archived from the original on 24 April 2011. Retrieved Mar 21, 2011. "Free Software Foundation employees have written and maintained a number of GNU software packages. Two notable ones are the C library and the shell. ... We funded development of these programs because the GNU Project was not just about tools or a development environment. Our goal was a complete operating system, and these programs were needed for that goal."
- ^ len ([email protected]) (April 20, 1993). "January 1993 GNU's Bulletin". gnu.announce. Web link. Retrieved Oct 28 2010.
- ^ Ramey, Chet (1994-08-01). "Bash - the GNU shell (Reflections and Lessons Learned)". Linux Journal. Archived from the original on 5 December 2008. Retrieved 2008-11-13.
- ^ Chet Ramey (October 31, 2010), Dates in your Computerworld interview, retrieved Oct 31 2010
- ^ Chet Ramey (June 12, 1989). "Bash 0.99 fixes & improvements". gnu.bash.bug. Web link. Retrieved Nov 1 2010.
- ^ Chet Ramey (July 24, 1989). "Some bash-1.02 fixes". gnu.bash.bug. Web link. Retrieved Oct 30 2010.
- ^ Brian Fox (March 2, 1990). "Availability of bash 1.05". gnu.bash.bug. Web link. Retrieved Oct 30 2010.
- ^ a b "6.11 Bash POSIX Mode", The GNU Bash Reference Manual, for Bash, Version 4.1, December 23, 2009, archived from the original on 3 December 2010, retrieved Oct 26 2010
- ^ The syntax matches that shown on the
regex(7)
man page. - ^ "The shell provides associative array variables, with the appropriate support to create, delete, assign values to, and expand them." http://tiswww.case.edu/php/chet/bash/NEWS
- ^ a b Mendel Cooper. "Portability Issues". The Linux Documentation Project. ibiblio.org.
- ^ http://www.hypexr.org/bash_tutorial.php#emacs
External links
Wikibooks has a book on the topic of: Bash Shell Scripting |
- Official website
- Bash Reference Manual (HTML PS PDF)
- Bash Guide for Beginners article at The Linux Documentation Project
- Advanced Bash Scripting Guide article at The Linux Documentation Project
- Linux Shell Scripting Tutorial (LSST) v2.0 wiki
- "The Comprehensive List of bash Reference Documentation and Examples"
- Useful Bash History Tips and Tricks
- 2008 interview with GNU Bash's maintainer, Chet Ramey
- Working with BASH environment variables
- Video Tutorial for Creating a Bash Script
- Bash commands and examples
- Colorized Bash prompt - how to set up a colorized Bash prompt based on the current connection method (SSH, telnet, etc.).
- jBash Project is a Java Parser for the Bourne Again Shell
- The 'official' channel FAQ for freenode's #bash channel is BashFAQ.
- BASHDB - Bash with a built-in debugger.
- Bash Quick Reference card
- List of Bash commands and environment variables (Built-in Commands External Commands Environment Variables)
|