Updated April 7, 2023
Introduction to Bash Shell Script
Bash is called an abrupt language that has not evolved much. To be very frank it is believed that bash was never intended to be developed. It was thought about a language in which a user would type in commands and the only job of bash is to execute them, and that is how it is used in shell scripting to empower the same to automate tasks which are like robotic executions on computer.
What is Bash Shell Script?
Bash is used as the base platform to execute the scripts. Bash is meant to be a collection of commands which when put together would create a beautiful small program, each having a specific role to accomplish in the world of automation or the intention with which the script is created. Did you realize that in the entire article we seldom say, “bash program” and rather say it as bash scripting? This is because, after a certain level of complexity the user starts feeling that the bash script is going out of hand!
Now, the very first question we would like to answer is, how do we create a bash script. The answer to this is simpler than the question itself. Just mention #!/bin/bash at the beginning of the file and voila, the script gets converted into bash shell script! Now once the script is converted into a bash script what happens next? A user might need to execute the script from the current directory where the script is located. When the script is executed, the path to the interpreter is searched for. For bash shell scripts, the interpreter is generally found under /bin/bash.
Features of Bash Shell Script
Below we would go through those features one by one in some detail so that you have an insightful understanding of the bash scripts.
- Variables: In bash scripting, very few features are capable of holding values and among them is variables. This is very much similar to variables in any other programming language. The job is to hold value given in the course of the code.
- Positional parameters: There are some keywords that act as a command to execute something special, assigned only to that variable. For example, mkdir helps in creating a directory.
- Exit status: To understand whether something has been executed successfully, there must be a status that must be relayed upon exit and that’s exactly what this feature takes care of.
- Conditional statements: This feature enables to write condition statements like an “if” condition so that one can execute only a set of commands given a certain condition is met.
- Loops: Bash scripts also enable the user to write loops like for, while, etc.
- Function: Bash script also gives the capability of writing a function, which essentially looks like a script within a script!
There are other features as well, but above are the ones that are very basic and are the backbone of any bash script. Another thing to mention is that bash is capable of handling arithmetic operators as well. some of the arithmetic operators are: + (addition), – (subtraction), * (multiplication), / (division), % (modulo), ** (exponential) along with operators for comparing values like < (less than), > (greater than), >= (greater than or equal to) and some other similar ones. Logical operators like && (AND), || (OR) are also available.
Where we Use Bash Shell Script?
Now one would ask where we use bash. What’s so special about bash that it is the undisputed leader in the tasks it is assigned to be done? So, let us answer where all bash is used.
- In case one needs to orchestrate shutdown or booting up tasks, bash comes in handy.
- In a folder, where a huge number of files are present, if the user has to rename the files to have some extra extension in all the files, the user would choose bash to complete the task.
- In the entire hard drive, if one has o find any duplicate file present saved elsewhere.
- To figure out if there is the presence of any weasel words in the writing, maybe for example in this article.
- Creation of a relational database of the files present.
With the above uses, it is quite evident that any form of repetitive task which won’t require much logic or human understanding to do can be achieved by bash using a shell script. Another point of trivia, did you know that bash is available directly as a shell via terminal in MacOS and Linux or Unix environment.
Examples to Implement Bash Shell Script
Below are the examples of Bash Shell Script:
Example #1
We would look at how the script is written and just print out a simple “Hello World” using our script. The code is as follows:
Code:
#!/bin/bash
echo "************************************************"
echo "Hello Fellas!"
echo "Learn to print out Hello World"
echo "************************************************"
echo "Powered by EduCBA"
echo "################################################"
echo "Printing : Hello World!!!!!"
Output:
If you carefully look at the very first line of the code, you would notice that the script is converted into a bash script by usage of #!/bin/bash.
Example #2
We would look at taking 2 inputs in the command line itself and then printing out the sum at the end. This example is closer to real world, where we would frequently need to do arithmetical operations. The code is as follows:
Code :
#!/bin/bash
echo "************************************************"
echo "Hello Fellas!"
echo "Here you would get to see how input from command line adds up using bash"
echo "************************************************"
echo "Powered by EduCBA"
echo "################################################"
for input in "$@"
do
#Saving the variable like X, Y in respective variables
var=$(echo $input | cut -f1 -d=)
val=$(echo $input | cut -f2 -d=)
case $var in
X) x=$val;;
Y) y=$val;;
*)
esac
done
#Summing up the values
((summation=x+y))
echo "X+Y=$summation"
Output :
Conclusion
As we took the final few minutes to sum up the learnings in this article, we have seen the way a shell script is converted into a bash script and then executed. One more thing which is quite general for running any shell script is to convert the permission of the script to allow execution for the script by using chmod +x <file_name>. As a summing up statement, bash is the medium through which we execute our shell script and it certainly has the reliability of ages of executing the scripts!
Recommended Articles
We hope that this EDUCBA information on “Bash Shell Script” was beneficial to you. You can view EDUCBA’s recommended articles for more information.