Bash is a commonly-used shell in many Linux distributions. Bash is a command interpreter. It is a command-line–only interface containing a handful of built-in commands; it has the ability to launch other programs and to control programs that have been launched from it (job control).
We can use bash to interact with the system.
Bash in Linux
Bash is a default shell now. It is very convenient. For example, they remember commands that we have typed and let us reuse those commands. It also let us edit those commands, so they don’t have to be the same each time. And bash let us define our own command abbreviations, shortcuts, and other features.
Bash is programmable. We can write a bash script to handle our daily work. Whenever we find ourselves doing a task repeatedly, we should try to automate it by writing a shell script. There are more powerful scripting languages, like Perl, Python, and Ruby, but the Linux shell bash is a great place to start. After all, we already know how to type commands.
Advantage of Bash
Bash is as powerful as other shells but adds convenience functions like the double brackets ([[ and ]]) in the sample code. These “Bashisms” are much loved by Bash users because they avoid the sometimes verbose and awkward syntax in other shells like tcsh or ash.
However, they are unique to Bash and are not POSIX-compliant, which could cause compatibility issues on systems not running Bash. Then again, Bash is open source free software, so most users can install it if they need it. The lack of compatibility only forces an extra dependency and does not exclude anyone from using a script.
How to use Bash in Linux
Most modern Linux and Unix distributions provide a Bash shell by default. They do this because Bash is well-known, and it has several convenience functions that other shells don’t.
However, some systems use another shell by default. To find out whether we are running a Bash shell, we can use the echo command along with a special variable representing the name of the currently running process:
$ echo $0
Understanding Bash Script in Linux
A Bash script is a plain text file which contains a series of commands. These commands are a mixture of commands we would normally type ouselves on the command line (such as ls or cp for example) and commands we could type on the command line but generally wouldn’t (you’ll discover these over the next few pages).
Anything we can run normally on the command line can be put into a script and it will do exactly the same thing. Similarly, anything we can put into a script can also be run normally on the command line and it will do exactly the same thing.
Linux Bash alternatives
On Linux, the interpreter that can understand the user commands and settings and pass them on to the computer for further processing is called a shell. Here are a few shells on Linux:
- sh – The Bourne shell is the first shell to be introduced on UNIX. Bash improves upon sh. All the sh commands can also be run on bash, but not all bash commands can be used on the sh shell. This makes bash a superset of sh and other options/modules.
- csh – The C shell with a syntax similar to the C programming language.
- tcsh – The TENEX C shell is the C shell with additional features.
- ksh – The Korn shell, a command and programming language.
Check Bash version in Linux
We can use this command to check the bash version in Linux.
% bash –version
GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin20)
Copyright (C) 2007 Free Software Foundation, Inc.