Introduction
In this tutorial, we will be looking into Bash Script – a tool used by developers to increase productivity and manage tasks. A lot of professionals use it for tasks such as system administration, data crunching, web app development, automated backups, etc. Bash is most commonly run on Linux OS.
What is a Bash Script?
In simple words, Bash Scripts are plain text files that contain a series of commands. These commands are the same ones that we type in a terminal command line. So the difference here is that instead of typing the commands directly on the command line, we type them in a plain text file. These files have a .sh extension.
Every bash script file starts with the line #!/bin/bash, where #! is referred to as the SheBang. The rest of the line consists of the path to the interpreter specifying the location of the bash shell in the OS.
The SheBang(#!) is known as the path directive. This is because it denotes an interpreter to execute the script lines and is used for the execution of scripts like Bash. NOTE: Be careful about the formatting of SheBang. Incorrect formatting can result in the malfunctioning of the script file.
Some common commands used in Bash are:
./ – For execution of a bash script
# – For commenting lines within the script
echo – For printing lines
Advantages of Bash Script
1. A major advantage is the simplicity of its creation. It can be made without any programming knowledge.
2. Bash Scripts can be automated, which helps in dealing with repetitive tasks easily.
3. It’s very time-saving and cost-effective
4. We can run multiple commands in the same script
5. We can create and edit text files using bash script
Bash Scripting – Examples
First, let’s start by creating a new bash script file. Remember that these files must contain the extension .sh in their file name. First, start the Linux terminal and type in cd Desktop/ to change the directory to Desktop. Next, type the command touch followed by the file name (eg. bScript.sh) to create our file as follows:
cd Desktop/
touch bScript.sh
Next, access the Desktop folder from the Files option. You will be able to see the newly created bash script file
1. Creating a Hello World Script
For every bash script file, we need to type #!/bin/bash as the first line. To display a line, we use the echo command followed by the text within double quotes
#!/bin/bash
echo "Hello World"
Following this, we go to the terminal to run the script file. However, this script file is not yet executable. Using the ls -l command, we can see the files and directories. Here, we can notice that the script name is not highlighted, indicating that it is not executable. So, we use the chmod +x command followed by the file name to make it executable as shown below:
cd Desktop/
ls -l
chmod +x bScript.sh
ls -l
Now, when we type ls -l command, we can see that the file name appears highlighted, which means that it is executable.
Next, we execute our Hello World script using ./ followed by the file name as shown below:
./bScript.sh
2. Printing strings on the same line (-n command)
Next, we will see how to display two strings on the same line. We use the -n command to implement this. The difference between using -n command and not using -n command is as follows:
#!/bin/bash
echo -n "With"
echo "-n command"
echo "With"
echo "no -n command"
cd Desktop/
./bScript.sh
3. Displaying multiple strings on the same line
It is possible to display multiple strings on the same line by typing the echo command followed by the strings in double quotes separated by space.
#!/bin/bash
echo "Using" "multiple" "strings" "on" "same" "line"
cd Desktop/
./bScript.sh
4. String continuation character (\)
In Bash, we use a backslash (\) as a string continuation character.
#!/bin/bash
echo "Using" "String" \
"continuation" "character"
The strings are written in two lines with just a single echo command. Normally, it would cause an error, but by adding a backslash, it is considered the same line. As a result, we get the following output:
cd Desktop/
./bScript.sh
5. Spacing words using tab character (\t)
We can space the words within a string using the tab character. For this, first, type the echo command followed by -e. Following this, type in the string with \t in between the words.
#!/bin/bash
echo -e "Using\ttab\tcharacter"
As a result, we get the following display:
cd Desktop/
./bScript.sh
6. Using the newline character (\n)
We can print the words within a single string in multiple lines by using the newline character. To do so, type the string with the words separated by \n.
#!/bin/bash
echo -e "Using\nnewline\ncharacter"
On executing the file, the result will be as follows:
cd Desktop/
./bScript.sh
7. Displaying words within single quotes and double quotes
In Bash Scripting, we can display words within single quotes in the normal way.
However, for words within double quotes, we need to type \” before and after the word./
#!/bin/bash
echo "Welcome to 'BashScript'"
echo "Welcome to \"BashScript\""
Following this, execute it on the terminal.
cd Desktop/
./bScript.sh
8. Displaying all the commands
Bash provides us with the option to display all the commands used in a script along with their result. This is by adding -x to the first line as follows:
#!/bin/bash -x
echo "Welcome"
echo "to"
echo "BashScript"
On executing the Script, we get the following display:
cd Desktop/
./bScript.sh
9. Writing to a text file using Script
Through the Bash Script, we are able create a new text file and write data into it. For writing a text into a text file, we use the > ./ symbols followed by the text file name. Look at the example below:
#!/bin/bash
echo "Hello World" > ./file.txt
Next, we execute the script file by calling it. Following this, we use the ls command to check the list of files created. If executed correctly, the name of the text file created will be displayed.
So, as the next step, we can view the contents of the text file using the nano editor. Use the nano command followed by the text file name.
cd Desktop/
./bScript.sh
ls
nano file.txt
As a result, the nano editor shows up as follows:
If you want to verify whether the text file is created, go to desktop folder as it is usually created in the location folder of the bash script file
10. Append text to a text file
Bash Script also provides us the option to append text to a text file. For this purpose, we type in the text followed by >> ./ and the text file name. Refer to the example given below:
#!/bin/bash
echo "Hello World!" > ./file.txt
echo "Welcome to the text file!" >> ./file.txt
Next, execute the script in the terminal
cd Desktop/
./bScript.sh
To test whether the text is added to the file, let’s check the text file once again.
11. Single Variable Usage
It is possible to create variables containing strings, integers, etc in bash script. We can display the value using $ symbol followed by the variable name. Let us look at an example for a better understanding.
#!/bin/bash
var="Hello!"
echo $var
var1=35
echo $var1
NOTE: Do not leave space between the variable name and the “=” operator. As a result of executing the script, we get the following display:
cd Desktop/
./bScript.sh
12. Multiple Variable Usage
Bash script also supports the usage of multiple variables. Look at the following example:
#!/bin/bash
var1="this is variable 1"
var2="this is variable 2"
var3="${var1} ${var2}"
echo "${var3}"
We get the following display on executing the script in the terminal
cd Desktop/
./bScript.sh
Conclusion
So, in this tutorial, we have looked into Bash Script in detail, and also demonstrated some examples to get started with bash scripting. Bash Scripting is a popular and powerful tool used by professionals to make their work easier. Hope that this tutorial was informative and evoked your curiosity about Bash Scripting.
To know more about bash scripts, you can check the official documentation by clicking here.
Happy Learning!