Updated April 10, 2023
What is Bash?
Bash is a type of program which is designed to communicate between the different programs easily. It is similar to other programs on our computer. The basic & main difference between bash and other programs is that it is designed to take input/command from the user while others have the ability to read files, do mathematical calculations, control devices and may more. As everything is governed by programming language, it is handled by “bash shell language”. Basically a shell program is the one that provides users to interact with other programs using an interface. There are different types of shell programs with their separate language. The popular shell programs are C shell(csh), Z shell(zsh), Bourne shell and Korn shell(ksh). It stands for Bourne Again shell, recently the popular available shell. Even though all of these shells use similar syntax, it is important to be aware of what shell we are actually writing code for.
History of Bash
The diagram clearly explains how does the bash come into existence:
Based on Linux and Window
Below is the detail about how it is based on Linux and Window:
Window
- In this bash is known as “win-bash”, a Window port of GNU bash. Like other bash port for Window (Example is Cygwin bash ), the win-bash does not require any special type of environment/DLLs, with only one binary file and that’s all.
- Win-bash developed on nt_bash, which was a bash port for Window NT earlier, initiated by Mountain Math Software a few years ago, which was never completed.
- The aim of the win-bash research was to provide the port to Windows which provides a fully-functional bash.exe binary for Windows NT and another derived system.
- Win-bash is usually used as an input shell along with used as an interpreter to run a Linux shell script.
Linux
In Linux, it is a command based where users interact with the interface. Let us discuss the in details in Linux :
Syntax:
bash -option filename
The command syntax is a superset of the Bourne shell command. This supports brace expansion, command line, basic debugging & signal handling.
Version
1. We can check the version by firing the command.
Code:
bash --version
Output:
From the above command output, we come across systems version number is 4.4.12(1).
2. We can also know the version by built-in $BASH_VERSION variable.
Code:
echo $BASH_VERSION
Output :
3. This version can be known from shell script also. Let us create a new file called:
Code:
~/check_bash_version.sh
-> cd ~/check_bash_version.sh
add the below lines in the file and save:
-> #! /bin/bash
-> echo " Bash version for machine \n"
-> echo " Bash version :${BASH_VERSION }"
Run the script by giving the permission:
-> chmod 777 check_bash_version.sh
-> ./check_bash_version.sh
Output:
List of Option
Let us discuss each option in details:
- -c Option: When the -c option is used then the command will read from the string. The argument after the string will be assigned to the positional parameter beginning with $0.
- -i Option: When the -i option is used the shell will in interactive mode.
- -l Option: This option stands for login shell and makes it act as if it is invoked for a login shell.
- -r Option: When the -r option is used then the shell will be in restricted mode.
- -s Option: The -s option will be used when there is no argument left after the option, processing done then the command will be read from the input standard. This feature of option enables the positional parameters to be set when we will invoke an interactive shell.
- -D Option: When we use -D option then a list of all double-quoted strings preceded by “$” will be printed on standard output console. These types string which is subject to language translation, if it is the present locale is not in C or POSIX. This signifies the -n option, no commands will be executed.
- — Option: A “–” signifies the end of option and stop the further option processing. Any argument passed after the — will be treated as filename and argument. An argument of – is similar to –.
Usage
- Whenever it starts it will execute the command in a variety of dotfiles and we need to have executed permission with #! bin/bash initialization
- When it is started in interactive mode. This read and execute /etc/profile (if there exist) or it will call the file /etc/bash.bashrc
- After reading the file, it will look for ~/.bash_login, ~/.bash_profile, and ~/.profile .It will read & execute in the sequence that will exist.
- ~/.bash_logout — When this is executed it will log out from the shell
- When it is started as an interactive shell but not as login shell then,
- bash read & execute /etc/bash.bashrc & then ~/.bashrc.
- This option –rcfile file forcefully makes to read & execute the command from the file instead of ~/.bashrc.
Recommended Articles
We hope that this EDUCBA information on “What is Bash?” was beneficial to you. You can view EDUCBA’s recommended articles for more information.