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.
The term bash is an acronym for “Bourne-again SHell,” which is a play on Stephen Bourne, the creator of the Bourne shell, as stated in the Bash Reference Manual. The Bourne shell programs are largely compatible with Bash, which is a superset of the older shell.
What is a shell?
If we are a new Linux user, and we open the terminal, it is assumed that we are well confused as to what to do with it. Here the Shell comes in the role.
The terminal contains the shell; it allows us to execute the commands to interact with the system. We can perform various operations such as store and retrieve data, process information, and various other simple as well as complex tasks.
To open the terminal, press CTRL+ALT+T keys. Perform some basic operations such as date, cal, ls, and pwd to take a tour with it.
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 glance, bash seems to be a straightforward command-and-response system in which users submit instructions and bash executes them and gives the results. However, bash also functions as a programming environment, allowing users to create scripts that use shell commands to create programs that receive input and output.
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
This command returns too many entries for humans to easily interpret, especially when it comes from the system root directory. Here, the user can pipe the output from the ls command to the grep command in Bash, which does text pattern matching.
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).
Features of Bash
All the built-in command of the sh shell is available in Bash; moreover, it facilitates us with many other features. Some key features of Bash are as follows:
- Shell Syntax: The shell syntax contains shell operations, quoting, and comments. The shell operations are the basic operation of the shell. Quoting allows how to remove the special meaning from characters, and comments are meant to specify the comments.
- Shell commands: Shell commands are the types of commands that you can execute. These commands can be simple commands, pipelines, lists, compound commands, and more.
- Shell Functions: Shell functions are used to group commands by name. They are executed as traditional commands. When we use the name of a shell function, the list of commands associated with that is executed.
- Shell parameters: Basically, a parameter is an entity that stores value; it can be a name, number, or special character. The shell parameters specify how the shell store value. They can be a positional parameter or a special parameter. Positional parameters are the shell’s command-line arguments, and the special parameters are denoted by a special character.
- Shell Expansions: Shell expansion is a technique that is used by Bash to expand the parameters. Expansion is performed on the command line after the input has been splitted into tokens.
- Redirections: It is a way to manage and control the input and output.
- Command execution: It decides how the system will react when we execute a command.
- Shell Scripts: It is a text file that has shell commands and executes them when it is used. Bash reads and executes the commands then exits.
One unique bash feature that isn’t always available with other CLIs is command-line editing. By hitting the up arrow key, Bash’s command history can be retrieved. This makes it simpler to execute a command again precisely. These previous commands can also be changed at the command line by copying, pasting, deleting, or changing a previous command using special keys.
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.