How to Create a Simple Shell Script in Linux
In Unix System, Shell provides an environment to connect the Linux user and operating system. It helps to get input from the user through commands, operate the function based on input and return the output on the terminal.
Shell scripts are the list of simple commands to perform operations. You can also create your own commands to perform functions in a system.
How to Create a Simple Shell Script in Linux:
Shell scripting can be added in a Linux system using multiple text editors. This guide will create a shell script with Linux by default text editor and the “Vim” text editor tool.
Let’s start:
Creating Shell Script Using Text Editor:
Creating shell scripting using a text editor is quite simple. What we need to do is follow the steps listed below:
Create a text file with the “.sh” extension and write a simple script in it:
Open the terminal in the same directory where you saved this file and give the executable access to the file through the command:
Execute the “testing.sh” script in terminal:
So, that was the simple technique to run the shell scripting file in the terminal.
You can also add the comments in the file using the”#” symbol.
Open the text editor and add comments using the “#” symbol:
Creating Shell Script Using Vim:
Vim is the popular and interesting text editor tool to create shell scripting files. If you don’t have it on the system, the installation command is given:
To open the editor, type the “vim” in the terminal:
Now, getting started with the “vim” text editor, open the terminal and create a bash file using the command:
When you executed this command, the editor will open like the image shown below with the file name “testing.sh” at the left bottom:
Press “i” to get the insert mode and write a script in it:
#! /bin/bash
echo “Welcome to the Linux family.”
To save it, press “Esc” to exit the insert mode and type “:w” to save the script:
The saved shell script will look like this:
Type “:q” getting back to the console and execute the shell file to show output in the terminal:
Add Comments in a Shell Script:
To add comments in a shell script, open the text file in Vim editor and write a comment using the “#” symbol:
#! /bin/bash
# This is the testing script!
echo “Welcome to the Linux family.”
Use Variables in a Shell Script:
In a programming world, variables play an important role in storing the input/output value.
To use the variables in a shell scripting file, let’s perform some examples:
#! /bin/bash
variable=“This is the testing shell script.”
echo $variable
Execute the command to get the variable value as an output:
Let’s do another example of for loop to print the numbers:
#! /bin/bash
for i in {1..7}
do
echo $i
done
Now, give access to the file to execute:
$ chmod +x script_example.sh
Run the command to display output in the terminal:
Conclusion:
In this guide, we have learned how to create a simple shell script in Linux systems. Shell scripts can be made using any text editor; whether you can use the system’s text editor or if you want to go with the tool, there are several available.
We have seen how to create shell script files, save them, and check multiple shell scripting examples using the Linux text editor and the Vim editor tool after installing it.
Don’t forget, Shell is a communication interface with the user and system, and the Shell scripts are used to perform useful tasks through commands.