Creating own script according to personal demand is the most fascinating thing a programmer wishes for. Bash gives us just that perfect option to create our own scripts in Linux. In this article, you will get to know what is bash in Linux and other interesting options of bash which will make you a pro user.
What is Bash in Linux?
There are two types of bash in Linux. Those are described below:
1. Bash as a Shell
Bash(Bourne Again Shell) is the most popular command language interpreter for the GNU operating system. It supports functions, variables, flow controls and also reads and executes commands from a file. It works as the middleman or shell between the computer and the human command line.
2. Bash as a command
Inside the bash shell, there is a bash command also. This command is used to run files or text windows.
1. Bash as a Command
1.1 Synopsis of Bash
On the man page of bash, you can find all the descriptions and uses of the bash command. It takes all the single-character shell options and a number of multi-character options. Bash pipes the command strings in a specified file.
2. Bash as a Shell
2.1 How to Run a Bash in Linux
Whenever you are using a terminal in our Linux operating system, you are using the bash every time. To run a bash shell in your machine you can follow some steps given below:
Steps to Follow >
➊ At first open the Ubuntu Terminal by pressing CTRL + ALT + T.
➋Type the following command in the command prompt:
echo $0
➌Press the ENTER button.
Output >
After that, you will be able to see that you are running bash as your shell or interpreter.
Note: If you don’t see this, it means you are running other shells. You can download and install bash if you want to use it.
2.2 Finding Bash in Linux
You need to know where the bash interpreter is located on your machine. To learn about it you should follow the steps given below:
Steps to Follow >
➊ Open the terminal first.
➋ Type the following command in the command prompt:
which bash
➌Press the ENTER button.
Output >
After that, you will be able to see the location of your bash interpreter. As you can see here, my bash shell is located in the /usr/bin/bash directory.
2.3 How to Make a Bash File in Linux
To make a bash file you should follow some procedures. Those are given below:
Steps to Follow >
➊ Open the terminal first.
➋ You can choose any text editor to open a file. Here I have used the nano text editor to open a bash file. I have given the following command in the command prompt:
nano hi
➌ After opening the nano file you have to give the following command first. This is a must to begin any bash file.
#!/bin/bash
Though other lines starting with a # sign do not have any particular meaning. They are just taken as a comment by the machine. Only this line starting with a #! (shebang) sign in the bash file has a particular meaning. So the second line in this file is just a comment, you don’t have to write it.
➍ Next, I have put three values. Those are:
echo “Hello, how are you? $USER”
echo “Today is $(date)”
echo “Have a good day”
I want to execute these three values. Here $USER gives us the user name of the machine. $(date) will show the date and time of the machine.
➎ Finally you just save the file using CTRL+S and exit the file press CTRL+X.
Output >
Now you come back to your command prompt. To run the bash file you have to type this command:
bash hi
You can see in the picture that three echo commands are executed correctly and got the desired values by just running the file.
Where borhan is the user of the machine and Mon Nov 28 4:421:38 PM is the current date and time.
Conclusion
After completing this article, you will be able to acquire all the features and aspects of bash in Linux. It will help you to understand how to run, find and make a bash file. With the help of this, you can write your own script and better your efficiency.
Similar Readings
FUNDAMENTALS A Complete Guide for Beginners