Getting Started with Bash Scripting in Linux
Bash, the default shell for the GNU operating system, is an advanced version of the Bourne/Unix shell. It facilitates complex tasks by allowing input and output operations, conditional statements, and loops, similar to other programming languages. Bash scripting involves automating routine tasks through command sequences within a script file. This article explores the creation and execution of bash scripts to simplify daily activities.
What is the Bash Shell?
A shell is an interface that is required to interact with the UNIX systems. It takes the inputs or commands from the user and performs the intended operation and then returns the output.
Shell is also known as a command line interface (CLI) because it uses the command line to interact with the system. The Bourne Again Shell, or Bash shell as it is more popularly known, is used by default. The shell serves as a bridge between the users and kernels.
A command entered by the user is converted by the shell into a call to the appropriate procedure or program. The bash scripts are saved in a file with an extension of “ .sh ”. The files are made executable using the chmod command and executed in the terminal.
Interactive Shell
A shell is called an interactive shell if it expects human intervention in the form of input of a command and then performs the desired operation to return an output. An interactive shell is something we can interact with, that is the operation will stop and it will not execute until it gets some input from the user.
Non-Interactive Shell
A shell is called a non-interactive shell if the shell need not require any intervention from a human. A non-interactive shell is running a script. The INIT and start-up scripts are considered non-interactive as they don’t require any input. Similarly, administrative scripts can also be considered non-interactive as they are executed in the background without any intervention.
What is Shebang?
If you would have seen any script you must have noticed something at the beginning of a script file there is a combination of the hash sign and an exclamation mark. It is also known as a sharp exclamation or sha-bang, she-bang hash-bang, pound-bang, or hash-piling.
This shebang is used to mark a text file as an executable file in Linux-based systems as well as define an interpreter to the shell. Shebang is necessary for a script as this marks the beginning of a script file. The syntax used in shebang is #! Path - arguments This tells the shell to execute the script with which shell/interpreter.
For example:
#!/bin/sh – This command defines the path to the interpreter or shell with which the script will be executed. For the scenario we are discussing, the location is /bin, and the shell is the Dash shell.
There might be some other keywords present that will direct the interpreter as to what to do.
Printing Hello World with Bash
As an initiation ritual, let's learn how to print the traditional "Hello world" string in the command line.
This command when executed in the shell will return the output string "Hello World".
Now we know how to print Hello World in shell let's move forward with using this command in Bash. First of all, create a file using the touch command.
This command creates a blank script with the name filename. sh
The next step is opening the file using an editor. You can use any editor you want; I have used nano editor for this tutorial.
As explained above the following command consists of shebang #! indicating that this file is an executable file and the next part /bin/sh indicating that the shell must be used to run the file.
The echo command will print the output "Hello World" when the script is run.
Congratulations you have created your first script!
Now to run the script first you need to provide adequate permission for the file to execute. The chmod command accomplishes this task.
This command provides execute permission to the given file.
For more detailed information on the chmod command, visit: here
Once the script has been made executable, you can directly run it using the following command.
Using Variables in Bash
Just like any other programming language shell scripts also use the concept of variables. Using variables we can store, modify, and reuse data throughout the script.
Once declared the value stored in the variable can access abe and be used to perform various other operations as well. A Sample script has been attached here which depicts the usage of variables.
The variable user_name stores the value of the string "Vipin" and when called using the $ symbol it returns the value the value of the variable and "Hello Vipin" is printed in the terminal when this script is saved and run in a .sh file.
Accepting Input Parameters in Bash
Similar to variables in Bash, we also have the option of accepting user input and using it in the program. This is accomplished using the command "read". It takes the input from the user and saves it into a variable.
This command will print the first line into the terminal and then wait for the user to input the value after receiving input from the user the last line will execute and print the Hello user_name, where user_name is the data from input.
You can use some other parameters such as read -sp available to even take passwords as input, which is not visible when typed.
Loops in Bash
Loops allow us to iterate over a particular set of instructions until a condition is met. Some of the loops available in Bash scripting are While loops, Until Loops, and For Loops.
While Loop: These Loops work until a particular condition is no more true.
Until Loop: The only difference between While Loop and Until Loop is that it checks whether the given condition is true and then performs some action but the while loop works until the condition is no more true.
For Loop: For loops are used in bash scripting to iterate over a block of code a known number of times which can be finite or infinite. There are multiple ways this for loop works, like iteration over an array, iteration over a range, etc.
Conditional Statements in Bash
As seen above Loops are generally guided by the use of Conditional Statements. Shell Scripting provides us the use of if statements if else statements if else if else statements, the use of nested if statements and even Case statements.
if: This is to check whether a given condition is true or not and based on that further action is done. Use of Multiple if conditions together are called nested if statements.
The if condition always ends with the keyword fi
else: This is to check whether the above-given statement if, else if is false or not. if both of them are false then by default the else case will run and execute the intended operation.
elif: This is to check whether the above given "if" statement is false so try this particular statement to check whether the given condition is satisfied or not.
case: Case is used to check and match a pattern between an already specified set of options available.
As seen in the conditional statement if, the case condition also ends with esac.
Conclusion
- A shell is an interface or bridge that is required to interact with the kernel.
- There are two types of shells: Interactive and Non-Interactive.
- Shebang is used to define an interpreter to the shell.
- Variables can be declared and used in Bash scripts to store, modify, and access data
- There are three loops in Bash scripting: For, Until, While
- Conditional Statements such as if, elif, else, and Case are available in Bash scripting