bash (Bourne again shell)
What is bash (Bourne Again Shell)?
Bash (Bourne Again Shell) is the free and enhanced version of the Bourne shell distributed with Linux and GNU operating systems. Bash is similar to the original, but has added features such as command-line editing.
Created to improve on the earlier Bourne shell (named sh), Bash includes features from the Korn shell and the C shell. Bash is intended to conform to the shell standard specified as part of IEEE POSIX. A command language script written for the Bourne shell should also run in the bash shell.
Bash is released under the GNU General Public License (GPL), and it is available for most versions of Unix and Linux and versions have been ported to MS-DOS and Windows.
As explained in the Bash Reference Manual, the name bash is an acronym of "Bourne-again SHell" which is a pun on Stephen Bourne, author of the Bourne shell. Bash is a superset of the earlier shell, and generally compatible with Bourne shell programs.
What is a shell?
In computing, a shell program provides access to an operating system's components. The shell gives users (or other programs) a way to get "inside" the system; the shell defines the boundary between inside and outside.
There are two types of operating system shells:
- Command-line interface (CLI) shells like bash offer users a concise and efficient mode of interacting with the OS, without requiring the overhead of a graphic user interface.
- Graphical user interface (GUI) shells, such as Windows and macOS, are considered easier for beginners to use, but usually also offer programs that emulate a CLI-based shell for system administrators or other power users who prefer to interact at a command prompt.
Bash is the most commonly used CLI shell for Unix-based OSes, including Linux.
What is bash used for?
Bash, like other CLIs, is used for any computer application that requires precision when working with files and data, especially where large numbers of files or large quantities of data need to be searched, sorted, manipulated or processed in any way.
Some of the most common Bash use cases include:
- System administrators use Bash to manage systems systematically and reproducibly. System administrators use Bash to troubleshoot systems that are not functioning as desired or expected by logging in to systems and reviewing system configurations and network connections. System administrators also rely on Bash scripts to distribute software updates and patches, to monitor running systems, and to update and configure systems.
- Software developers rely on Bash for many development tasks. Bash can be used to automate software development tasks such as code compilation, debugging source code, change management and software testing.
- Network engineers use Bash to test, configure and optimize network performance on organizational networks.
- Computer science researchers use Bash to manage research systems and to carry out research on those systems.
- Hobbyists and power users use Bash to interact with their systems, execute programs and maintain their systems.
Bash is commonly used interactively, but it can also be used to write shell scripts. Almost any computer task can be automated using a Bash script. Bash scripts can be run on-demand or scheduled to run periodically.
How does bash work?
At first sight bash appears to be a simple command/response system, where users enter commands and bash returns the results after those commands are run. However, bash is also a programming platform and users are enabled to write programs that accept input and produce output using shell commands in shell scripts.
One of the most basic bash commands, ls, does one thing: list directory contents. By itself this command lists only the names of files and subdirectories in the current working directory.
The ls command has numerous parameters that modify how the results are displayed. Some frequently used parameters used with the ls command include:
ls command-line arguments (parameters) |
Purpose |
-l |
Use a longer, more detailed, listing format to include file permissions, file owner, group, size and date/time of creation. |
-a |
List all files and subdirectories, even those that are ordinarily intended to be hidden. |
-s |
Display the size of each file. |
-h |
Display file and subdirectory sizes in human-readable format using K, M, G and so on to indicate kilobytes, megabytes and gigabytes. |
-R |
Recursive listing of all files and subdirectories under the current working directory. |
Used all together, these parameters give the user a much clearer sense of what files and subdirectories are in a directory, when they have last been changed and by whom.
Bash enables combining commands by piping output of one command to be used as the input for another command. For example, this command can be used to list all files on a file system using the -R parameter to specify the listing should be recursive:
user@hostname:/$ 1s -1ashR
The response to this command, especially when issued from the system root directory, contains too many entries for humans to parse easily. This is where pipes can come into play, in this case, the user can pipe the output from the ls command to the Bash text pattern matching command, grep.
The pipe symbol (vertical bar, or "|") directs output from the directory listing into the grep command to return only files and subdirectories with filenames that include the specified text pattern. This command:
user@hostname:/$ 1s -1ashR |grep ‘filename.txt’
returns only files that include the string 'filename.txt' so this command can be used to locate a specific file.
Some things that are much easier to do interactively from the bash command line include:
- file and directory management;
- checking on network configuration;
- editing a configuration file (or any text file); and
- showing the difference between two files.
Types of bash commands include:
- Simple commands, which usually are run by themselves or with parameters and variables. For example, the ls command takes parameters as well as variables relating to the directories or files to be listed.
- Pipes, which are used to link the output of one or more commands as input to other commands.
- Lists, which enable users to run multiple commands in sequence.
- Compound commands, which enable script programming and include loops (for repeating a command a specific number of times) and conditional constructs (for running commands only when a specific condition is met).
Command-line editing is one special bash feature not always available with other CLIs. Bash retains a command history, which can be accessed by pressing the up arrow key. This makes it easier to precisely rerun a command. These prior commands can also be modified at the command line, using special keys to copy, paste, delete or modify a prior command.
Bash is one of the foundations of modern system and network administration, and new users face a learning curve when using it. However, once learned, bash skills are forever: a time-traveling system administrator from 1992 would likely be able to get right back to work on a modern Linux system, using bash. Learn more about how Bash scripting from this tutorial on creating a bash shell that accepts arguments when it runs.