Bash is a
free softwareFree software, software libre or libre software is software that can be used, studied, and modified without restriction, and which can be copied and redistributed in modified or unmodified form either without restriction, or with minimal restrictions only to ensure that further recipients can also...
Unix shellA Unix shell is a command-line interpreter and script host that provides a traditional user interface for the Unix operating system and for Unix-like systems...
written for the
GNU ProjectThe GNU Project is a free software, mass collaboration project, announced on September 27 1983, by Richard Stallman at MIT. It initiated the GNU operating system, software development for which began in January 1984...
. Its name is an acronym which stands for
Bourne-again shell. The name is a
punA pun, or paronomasia, is a form of word play that deliberately exploits ambiguity between similar-sounding words for humorous or rhetorical effect...
on the name of the
Bourne shellThe Bourne shell, or sh, was the default Unix shell of Unix Version 7, and replaced the Thompson shell, whose executable file had the same name, sh. It was developed by Stephen Bourne, of AT&T Bell Laboratories, and was released in 1977 in the Version 7 Unix release distributed to colleges and...
(sh), an early and important Unix shell written by Stephen Bourne and distributed with
Version 7 UnixSeventh Edition Unix, also called Version 7 Unix, Version 7 or just V7, was an important early release of the Unix operating system. V7, released in 1979, was the last Bell Laboratories release to see widespread distribution before the commercialization of Unix by AT&T in the early 1980s...
circa 1978, and "born again". Bash was created in 1987 by
Brian FoxBrian J. Fox is a computer programmer, entrepreneur, consultant, author, and free software advocate. In 1987, he authored the GNU Bash shell, a common Unix operating system interface....
. In 1990
Chet RameyChet Ramey is the current maintainer of the GNU Bourne Again Shell and GNU Readline. He is a longtime employee of Case Western Reserve University, from which he also received his B.S. in Computer Engineering and Master's degree in Computer Science....
became the primary maintainer.
Bash is the shell for the GNU operating system from the GNU Project. It can be run on most
Unix-likeA Unix-like operating system is one that behaves in a manner similar to a Unix system, while not necessarily conforming to or being certified to any version of the Single UNIX Specification....
operating systems. It is the default shell on most systems built on top of the
Linux kernelThe Linux kernel is an operating system kernel used by the Linux family of Unix-like operating systems. It is one of the most prominent examples of free and open source software....
as well as on
Mac OS XMac OS X is a line of computer operating systems developed, marketed, and sold by Apple Inc., and since 2002 has been included with all new Macintosh computer systems...
and
DarwinDarwin is an open source POSIX-compliant computer operating system released by Apple Inc. in 2000. It is composed of code developed by Apple, as well as code derived from NEXTSTEP, BSD, and other free software projects....
. It has also been ported to
Microsoft WindowsMicrosoft Windows is a series of software operating systems and graphical user interfaces produced by Microsoft. Microsoft first introduced an operating environment named Windows in November 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces...
using Subsystem for UNIX-based Applications (SUA), or
POSIXPOSIX or "Portable Operating System Interface [for Unix"] is the name of a family of related standards specified by the IEEE to define the application programming interface , along with shell and utilities interfaces for software compatible with variants of the Unix operating system, although the...
emulation provided by
CygwinCygwin is a Unix-like environment and command-line interface for Microsoft Windows. Cygwin provides native integration of Windows-based applications, data, and other system resources with applications, software tools, and data of the Unix-like environment...
and
MSYSMinGW , formerly mingw32, is a native software port of the GNU Compiler Collection to Microsoft Windows, along with a set of freely distributable import libraries and header files for the Windows API. MinGW allows developers to create native Microsoft Windows applications...
. It has been ported to
MS-DOSMS-DOS is an operating system developed by Microsoft. It was the most commonly used member of the DOS family of operating systems and was the main operating system for personal computers during the 1980s. It was preceded by M-DOS , designed and copyrighted by Microsoft in 1979...
by the
DJGPPDJGPP is a 32-bit C/C++/ObjC/ObjC++/Ada/Fortran development suite for 386+ PCs that runs under DOS or compatibles. It is guided by DJ Delorie, who started the project in 1989. It is a port of the popular gcc compiler, as well as many other GNU utilities such as bash, find, tar, ls, mv, awk, sed,...
project and to
Novell NetWareNetWare is a network operating system developed by Novell, Inc. It initially used cooperative multitasking to run various services on a personal computer, and the network protocols were based on the archetypal Xerox Network Services stack....
.
Features
The Bash
commandIn computing, a command is a directive to a computer program acting as an interpreter of some kind, in order to perform a specific task. Most commonly a command is a directive to some kind of command line interface, such as a shell....
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 referencing a Bourne special variable or those using
builtinIn computing, a shell builtin is a command or a function, called from a shell, that is executed directly in the shell itself, instead of an external executable program which the shell would load and execute....
Bourne commands. Bash command syntax includes ideas drawn from the
Korn shellThe Korn shell is a Unix shell which was developed by David Korn in the early 1980s. It is backwards-compatible with the Bourne shell and includes many features of the C shell as well, such as a command history, which was inspired by the requests of Bell Labs users.The main advantage of ksh over...
(ksh) and the
C shellThe C shell is a Unix shell developed by Bill Joy for the BSD Unix system. The scripting syntax is modeled after the C programming language. Earlier releases of OpenBSD provided the C shell as the default shell on the system. The C shell was derived from the 6th Edition Unix Thompson shell which...
(csh) such as command line editing,
command historyCommand history is a common feature in operating system shells , computer algebra programs, and other software that interacts with the user through a command line interface. Command history involves making previously-entered commands, usually up to some limit, easy to once again input to the...
, the directory stack, the
$RANDOM and
$PPID variables, and
POSIXPOSIX or "Portable Operating System Interface [for Unix"] is the name of a family of related standards specified by the IEEE to define the application programming interface , along with shell and utilities interfaces for software compatible with variants of the Unix operating system, although the...
command substitution syntax
$(…). When used as an interactive command shell and pressing the
tab keyTab key on a keyboard is used to advance the cursor to the next tab stop.- Origin :When a person wanted to type a table on a typewriter, there was a lot of time-consuming and repetitive use of the space bar and backspace key. To simplify this, a bar was placed in the mechanism with a moveable...
, Bash automatically uses
command line completionCommand line completion is a common feature of command line interpreters, in which the program automatically fills in partially typed commands....
to match partly typed program names, filenames and variable names.
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 function declarations (using the key word 'function') are not compatible with Bourne/Korn/POSIX/C-shell scripts, so Bash shell scripts are almost never compatible with other common shells.
Bash supports here documents just as the Bourne shell always has. However, since version 2.05b Bash can redirect standard input (stdin) from a "here string" using the
<<< operator.
Bash 3.0 supports in-process
regular expressionIn computing, regular expressions provide a concise and flexible means for identifying strings of text of interest, such as particular characters, words, or patterns of characters...
matching using a syntax reminiscent of
PerlPerl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall, a linguist working as a systems administrator for NASA, in 1987, as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone...
.
Brace expansion
Brace expansion is a feature, originating in
cshThe C shell is a Unix shell developed by Bill Joy for the BSD Unix system. The scripting syntax is modeled after the C programming language. Earlier releases of OpenBSD provided the C shell as the default shell on the system. The C shell was derived from the 6th Edition Unix Thompson shell which...
, that allows arbitrary strings to be generated using a similar technique to filename expansion. However the generated names need not exist as files. The results of each expanded string are not sorted and left to right order is preserved:
- This is a bash-specific feature
echo a{p,c,d,b}e # ape ace ade abe
Brace expansions should not be used in portable shell scripts, because a traditional shell will not produce the same output:
- A traditional shell does not produce the same output
echo a{p,c,d,b}e # a{p,c,d,b}e
Startup scripts
When Bash starts, it executes the commands in a variety of different scripts.
When Bash is invoked as an interactive login shell, it first reads and executes commands from the file
/etc/profile, if that file exists. After reading that file, it looks for
~/.bash_profile,
~/.bash_login, and
~/.profile, in that order, and reads and executes commands from the first one that exists and is readable.
When a login shell exits, Bash reads and executes commands from the file
~/.bash_logout, if it exists.
When an interactive shell that is not a login shell is started, Bash reads and executes commands from
~/.bashrc, if that file 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
Shell scripts written with Bash-specific features (
bashisms) will not function on a system using the Bourne shell or one of its replacements, unless Bash is installed as a secondary shell and the script begins with
#!/bin/bash (known as the "
shebangIn computing, a shebang refers to the characters "#!" when they are the first two characters in a text file...
line"). This problem became particularly important when
UbuntuUbuntu , is a computer operating system based on the Debian Linux distribution. It is named after the South African ethical ideology Ubuntu and is distributed as free and open source software. Ubuntu provides an up-to-date, stable operating system for the average user, with a strong focus on...
began to ship the
Debian Almquist shellDebian Almquist shell is a Unix shell, much smaller than bash but still aiming at POSIX-compliancy. It requires less disk space but is also less feature-rich. Some missing features, such as the $LINENO variable , are required by POSIX.Dash is a direct descendant of the NetBSD version...
(dash) as the default scripting shell in October 2006, causing a wide variety of scripts to fail.
Keyboard shortcuts
:
AutocompleteAutocomplete is a feature provided by many web browsers, e-mail programs, source code editors, database query tools, word processors, and command line interpreters. Autocomplete involves the program predicting a word or phrase that the user wants to type in without the user actually typing it in...
s from the cursor position. : moves the cursor at the beginning of the line (equivalent to the key :
HomeThe home key is a key commonly found on computer keyboards. The key has the opposite effect of the end key.-Microsoft Windows:In modern Microsoft Windows text editing applications, it is primarily used to return the cursor to the beginning of the line where the cursor is located...
). : (end) moves the cursor at the line end (equivalent to the key :
EndThe end key is a key commonly found on computer keyboards. The key has the opposite effect of the home key.-Microsoft Windows:In modern Microsoft Windows text editing applications, it is primarily used to put the cursor at the end of the line in which the cursor is...
). : (previous) recalls the previous command (equivalent to the key :
Up arrowCursor movement keys or arrow keys are buttons on a computer keyboard that are either programmed or designated to move the cursor in a specified direction....
). : (next) recalls the next command (equivalent to the key :
Down arrowCursor movement keys or arrow keys are buttons on a computer keyboard that are either programmed or designated to move the cursor in a specified direction....
). : (research) recalls the last command including the specified character(s) (equivalent to :
vim ~/.bash_history). A second
CTRL + r recalls the next anterior command which corresponds to the research : 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). : executes the found command from research. : clears the screen content (equivalent to the command :
clear' is a standard Unix computer operating system command which is used to clear the screen.Depending on the system,
clear uses the terminfo or termcap database, as well as looking into the environment for the terminal type in order to deduce how to clear the screen...
). : clears the line content before the cursor and copy it in the
clipboardThe clipboard is a software facility that can be used for short-term data storage and/or data transfer between documents or applications, via copy and paste operations...
. : clears the line content after the cursor and copy it in the
clipboardThe clipboard is a software facility that can be used for short-term data storage and/or data transfer between documents or applications, via copy and paste operations...
. : clears the word before the cursor and copy it in the
clipboardThe clipboard is a software facility that can be used for short-term data storage and/or data transfer between documents or applications, via copy and paste operations...
. : (yank) adds the
clipboardThe clipboard is a software facility that can be used for short-term data storage and/or data transfer between documents or applications, via copy and paste operations...
content from the cursor position. : sends an EOF marker, which (unless disabled by an option) closes the current
shellA shell is a piece of software that provides an interface for users. Typically, the term refers to an operating system shell which provides access to the services of a kernel...
(equivalent to the command :
Exitexit is a command used in many operating system command line shells and scripting languages. The command causes the shell or program to terminate. If performed within an interactive command shell, the user is logged out of their current session, and/or user's current console or...
). : sends the signal
SIGINTOn POSIX-compliant platforms, SIGINT is the signal sent to a process by its controlling terminal when a user wishes to interrupt the process. In source code, SIGINT is a symbolic constant defined in the header file signal.h...
to the current task, which aborts and close it. : sends the signal
SIGTSTPOn POSIX-compliant platforms, SIGTSTP is the signal sent to a process by its controlling terminal when the user requests that the process be suspended. The symbolic constant for SIGTSTP is defined in the header file signal.h. Symbolic signal names are used because signal numbers can...
to the current task, which suspends it. To return to it after one can enter
fg 'process name' (foreground). : (because x has a crossing shape) alternates the cursor with its old position. : (editor because it takes the $EDITOR shell variable) edits the current line in
vivi is a family of screen-oriented text editors which share common characteristics, such as methods of invocation from the operating system command interpreter, and characteristic user interface features...
. : (forward) moves forward the cursor of one word. : (backward) moves backward the cursor of one word. : cuts the word before the cursor. : cuts the word after the cursor. : capitalizes every character from the cursor's position to the end of the current word. : lowers the case of every character from the cursor's position to the end of the current word. : capitalizes the character under the cursor and moves to the end of the word. : cancels the changes and put back the line as it was in the history.
See also
- Comparison of command shells
External links