Bash
Encyclopedia
Bash is a Unix shell
Unix shell
A Unix shell is a command-line interpreter or shell that provides a traditional user interface for the Unix operating system and for Unix-like systems...

 written by Brian Fox for the GNU Project
GNU Project
The GNU Project is a free software, mass collaboration project, announced on September 27, 1983, by Richard Stallman at MIT. It initiated GNU operating system development in January, 1984...

 as a free software
Free software
Free 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 restrictions that only ensure that further recipients can also do...

 replacement for the Bourne shell
Bourne shell
The Bourne shell, or sh, was the default Unix shell of Unix Version 7 and most Unix-like systems continue to have /bin/sh - which will be the Bourne shell, or a symbolic link or hard link to a compatible shell - even when more modern shells are used by most users.Developed by Stephen Bourne at AT&T...

 (sh). Released in 1989, it has been distributed widely as the shell for the GNU operating system and as the default shell on Linux
Linux
Linux is a Unix-like computer operating system assembled under the model of free and open source software development and distribution. The defining component of any Linux system is the Linux kernel, an operating system kernel first released October 5, 1991 by Linus Torvalds...

, Mac OS X
Mac OS X
Mac OS X is a series of Unix-based operating systems and graphical user interfaces developed, marketed, and sold by Apple Inc. Since 2002, has been included with all new Macintosh computer systems...

 and Darwin
Darwin (operating system)
Darwin 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 been ported to Microsoft Windows
Microsoft Windows
Microsoft Windows is a series of operating systems produced by Microsoft.Microsoft introduced an operating environment named Windows on November 20, 1985 as an add-on to MS-DOS in response to the growing interest in graphical user interfaces . Microsoft Windows came to dominate the world's personal...

 and distributed with Cygwin
Cygwin
Cygwin 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 MinGW
MinGW
MinGW , formerly mingw32, is a native software port of the GNU Compiler Collection and GNU Binutils for use in the development of native Microsoft Windows applications; MinGW can function either as a cross compiler targeting Windows or as a native toolchain run on Windows itself...

, to DOS
DOS
DOS, short for "Disk Operating System", is an acronym for several closely related operating systems that dominated the IBM PC compatible market between 1981 and 1995, or until about 2000 if one includes the partially DOS-based Microsoft Windows versions 95, 98, and Millennium Edition.Related...

 by the DJGPP
DJGPP
DJGPP is a development suite for 386+ IBM PC compatibles which supports DOS-enabled operating systems. It is guided by DJ Delorie, who began the project in 1989. It is a port of the popular GCC compiler, as well as mostly GNU utilities such as bash, find, tar, ls, awk, sed, and ld to DPMI...

 project and to Novell NetWare
Novell NetWare
NetWare is a network operating system developed by Novell, Inc. It initially used cooperative multitasking to run various services on a personal computer, with network protocols based on the archetypal Xerox Network Systems stack....

.

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
Shell script
A shell script is a script written for the shell, or command line interpreter, of an operating system. It is often considered a simple domain-specific programming language...

. Like all Unix shells, it supports filename wildcarding
Wildcard character
-Telecommunication:In telecommunications, a wildcard character is a character that may be substituted for any of a defined subset of all possible characters....

, piping
Pipeline (Unix)
In Unix-like computer operating systems , a pipeline is the original software pipeline: a set of processes chained by their standard streams, so that the output of each process feeds directly as input to the next one. Each connection is implemented by an anonymous pipe...

, here documents, command substitution
Command substitution
In computing, command substitution is a facility originally introduced in the Unix shells that allows a command to be run and its output to be pasted back on the command line as arguments to another command...

, variables
Variable (programming)
In computer programming, a variable is a symbolic name given to some known or unknown quantity or information, for the purpose of allowing the name to be used independently of the information it represents...

 and control structures
Control flow
In computer science, control flow refers to the order in which the individual statements, instructions, or function calls of an imperative or a declarative program are executed or evaluated....

 for condition-testing and iteration
Iteration
Iteration means the act of repeating a process usually with the aim of approaching a desired goal or target or result. Each repetition of the process is also called an "iteration," and the results of one iteration are used as the starting point for the next iteration.-Mathematics:Iteration in...

. The keywords
Keyword (computer programming)
In computer programming, a keyword is a word or identifier that has a particular meaning to the programming language. The meaning of keywords — and, indeed, the meaning of the notion of keyword — differs widely from language to language....

, syntax and other basic features of the language were all copied from sh. Other features, e.g., history, were copied from csh
C shell
The C shell is a Unix shell that was created by Bill Joy while a graduate student at University of California, Berkeley in the late 1970s. It has been distributed widely, beginning with the 2BSD release of the BSD Unix system that Joy began distributing in 1978...

 and ksh
Korn shell
The Korn shell is a Unix shell which was developed by David Korn in the early 1980s and announced at USENIX on July 14, 1983. Other early contributors were AT&T Bell Labs developers Mike Veach, who wrote the emacs code, and Pat Sullivan, who wrote the vi code...

. Bash is a POSIX
POSIX
POSIX , an acronym for "Portable Operating System Interface", is a family of standards specified by the IEEE for maintaining compatibility between operating systems...

 shell but with a number of extensions.

The name itself is an acronym, a pun
Pun
The pun, also called paronomasia, is a form of word play which suggests two or more meanings, by exploiting multiple meanings of words, or of similar-sounding words, for an intended humorous or rhetorical effect. These ambiguities can arise from the intentional use and abuse of homophonic,...

 and descriptive. As an acronym, it stands for Bourne-again shell, referring to its objective as a free replacement for the Bourne shell.
As a pun, it expressed that objective in a phrase that sounds the same as born again, a term for spiritual rebirth. The name is also descriptive of what it did, bashing together the features of sh, csh
C shell
The C shell is a Unix shell that was created by Bill Joy while a graduate student at University of California, Berkeley in the late 1970s. It has been distributed widely, beginning with the 2BSD release of the BSD Unix system that Joy began distributing in 1978...

 and ksh
Korn shell
The Korn shell is a Unix shell which was developed by David Korn in the early 1980s and announced at USENIX on July 14, 1983. Other early contributors were AT&T Bell Labs developers Mike Veach, who wrote the emacs code, and Pat Sullivan, who wrote the vi code...

.

History

Brian Fox began coding
Computer programming
Computer programming is the process of designing, writing, testing, debugging, and maintaining the source code of computer programs. This source code is written in one or more programming languages. The purpose of programming is to create a program that performs specific operations or exhibits a...

 Bash on January 10, 1988 after Richard Stallman
Richard Stallman
Richard Matthew Stallman , often shortened to rms,"'Richard Stallman' is just my mundane name; you can call me 'rms'"|last= Stallman|first= Richard|date= N.D.|work=Richard Stallman's homepage...

 became dissatisfied with the lack of progress being made by a prior developer. Stallman and the Free Software Foundation
Free Software Foundation
The Free Software Foundation is a non-profit corporation founded by Richard Stallman on 4 October 1985 to support the free software movement, a copyleft-based movement which aims to promote the universal freedom to create, distribute and modify computer software...

 (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. Fox released Bash as a beta, version .99, on June 7, 1989 and remained the primary maintainer until sometime between mid-1992 and mid-1994, when he was laid off from FSF and his responsibility was transitioned to another early contributor, Chet Ramey.

Features

The Bash command
Command (computing)
In 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
SuperSet
SuperSet Software was a group founded by friends and former Eyring Research Institute co-workers Drew Major, Dale Neibaur, Kyle Powell and later joined by Mark Hurst...

 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
Korn shell
The Korn shell is a Unix shell which was developed by David Korn in the early 1980s and announced at USENIX on July 14, 1983. Other early contributors were AT&T Bell Labs developers Mike Veach, who wrote the emacs code, and Pat Sullivan, who wrote the vi code...

 (ksh) and the C shell
C shell
The C shell is a Unix shell that was created by Bill Joy while a graduate student at University of California, Berkeley in the late 1970s. It has been distributed widely, beginning with the 2BSD release of the BSD Unix system that Joy began distributing in 1978...

 (csh) such as command line editing, command history
Command History
Command history is a feature in many operating system shells, computer algebra programs, and other software that allows the user to recall, edit and rerun previous commands....

, the directory stack, the $RANDOM and $PPID variables, and POSIX command substitution
Command substitution
In computing, command substitution is a facility originally introduced in the Unix shells that allows a command to be run and its output to be pasted back on the command line as arguments to another command...

 syntax $(…). When used as an interactive command shell and pressing the tab key
Tab key
Tab key on a keyboard is used to advance the cursor to the next tab stop.- Origin :The word tab derives from the word tabulate, which means "to arrange data in a tabular, or table, form"...

, Bash automatically uses command line completion
Command line completion
Command 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. The Bash commmand-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
Process substitution
In computing, process substitution is a form of inter-process communication that allows the input or output of a command to appear as a file. The command is substituted in-line, where a file name would normally occur, by the command shell...

 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, Bash conformance with POSIX is nearly perfect.

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 expression
Regular expression
In computing, a regular expression provides a concise and flexible means for "matching" strings of text, such as particular characters, words, or patterns of characters. Abbreviations for "regular expression" include "regex" and "regexp"...

 matching using a syntax reminiscent of Perl
Perl
Perl is a high-level, general-purpose, interpreted, dynamic programming language. Perl was originally developed by Larry Wall in 1987 as a general-purpose Unix scripting language to make report processing easier. Since then, it has undergone many changes and revisions and become widely popular...

.

Bash 4.0 supports associative arrays allowing faked support for multi-dimensional arrays, in a similar way to awk:


declare -A a # declare an associative array 'a'
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
C shell
The C shell is a Unix shell that was created by Bill Joy while a graduate student at University of California, Berkeley in the late 1970s. It has been distributed widely, beginning with the 2BSD release of the BSD Unix system that Joy began distributing in 1978...

 that generates the 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.

  1. !/bin/sh

  1. 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, 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.
Some versions of Unix have especially contorted system scripts for Bash which violate the documented script load order (by loading scripts too early or attempting to combine Bash startup with the startup scripts for other shells in various ways).

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 also installed and the script begins with a "shebang
Shebang (Unix)
In computing, a shebang is the character sequence consisting of the characters number sign and exclamation point , when it occurs as the first two characters on the first line of a text file...

 line" of #!/bin/bash interpreter directive
Interpreter directive
An interpreter directive is a computer language construct that is used to control which interpreter parses and interprets the instructions in a computer program.- See also :* Shebang * Bourne-Again Shell* C Shell...

 instead of #!/bin/sh.

Keyboard shortcuts

The following shortcuts work when using default (Emacs
Emacs
Emacs is a class of text editors, usually characterized by their extensibility. GNU Emacs has over 1,000 commands. It also allows the user to combine these commands into macros to automate work.Development began in the mid-1970s and continues actively...

) key bindings. Vi
Vim (text editor)
Vim is a text editor written by Bram Moolenaar and first released publicly in 1991. Based on the vi editor common to Unix-like systems, Vim is designed for use both from a command line interface and as a standalone application in a graphical user interface...

-bindings can be enabled by running set -o vi.
: Autocomplete
Autocomplete
Autocomplete is a feature provided by many web browsers, e-mail programs, search engine interfaces, 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...

s from the cursor position. : Moves the cursor to the line start (equivalent to the key ). : Moves the cursor back one character. : Sends the signal SIGINT
SIGINT (POSIX)
On 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 closes it. : Sends an EOF marker, which (unless disabled by an option) closes the current shell
Shell (computing)
A shell is a piece of software that provides an interface for users of an operating system which provides access to the services of a kernel. However, the term is also applied very loosely to applications and may include any software that is "built around" a particular component, such as web...

 (equivalent to the command exit
Exit (command)
exit 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 terminal connection...

). (Only if there is no text on the current line) : Deletes the current character. (Only if there is text on the current line) : (end) moves the cursor to the line end (equivalent to the key ). : Moves the cursor forward one character. : Abort the research and restore the original line. : Deletes the previous character (same as backspace). : Equivalent to the tab key. : Equivalent to the enter key. : Clears the line content after the cursor and copies it into the clipboard
Clipboard (software)
The 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 screen content (equivalent to the command clear
Clear (Unix)
' 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. The Unix command clear takes no...

). : (next) recalls the next command (equivalent to the key ). : Executes the found command from history, and fetch the next line relative to the current line from the history for editing. : (previous) recalls the prior command (equivalent to the key ). : Add the next character typed to the line verbatim. : (research) recalls the last command including the specified character(s). A second 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). If you changed that XOFF setting, use to return. : Transpose the previous two characters. : Clears the line content before the cursor and copies it into the clipboard
Clipboard (software)
The 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...

. : Add the next character typed to the line verbatim. : Clears the word before the cursor and copies it into the clipboard
Clipboard (software)
The 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 clipboard
Clipboard (software)
The 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. : Edits the current line in the $EDITOR program, or vi
Vi
vi is a screen-oriented text editor originally created for the Unix operating system. The portable subset of the behavior of vi and programs based on it, and the ex editor language supported within these programs, is described by the Single Unix Specification and POSIX.The original code for vi...

 if undefined. : Read in the contents of the inputrc file, and incorporate any bindings or variable assignments found there. : Incremental undo, separately remembered for each line. : Display version information about the current instance of bash. : Alternates the cursor with its old position. (C-x, because x has a crossing shape). : Sends the signal SIGTSTP
SIGTSTP
SIGTSTP is a signal in a Unix computer system that tells a program to stop temporarily. On 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...

 to the current task, which suspends it. To execute it in background one can enter bg. To bring it back from background or suspension fg ['process name or job id'] (foreground) can be issued. : Incremental undo, separately remembered for each line. : (backward) moves backward the cursor of one word. : Capitalizes the character under the cursor and moves to the end of the word. : Cuts the word after the cursor. : (forward) moves forward the cursor of one word. : Lowers the case of every character from the cursor's position to the end of the current word. : Cancels the changes and put back the line as it was in the history. : Capitalizes every character from the cursor's position to the end of the current word. : Insert the last argument to the previous command (the last word of the previous history entry).

External links



The source of this article is wikipedia, the free encyclopedia.  The text of this article is licensed under the GFDL.
 
x
OK