How to Create a Simple Shell Script in Linux

15

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:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837244 101 How to Create a Simple Shell Script in Linux

Execute the “testing.sh” script in terminal:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837245 608 How to Create a Simple Shell Script in Linux

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:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837245 838 How to Create a Simple Shell Script in Linux

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:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837246 709 How to Create a Simple Shell Script in Linux

To open the editor, type the “vim” in the terminal:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837246 101 How to Create a Simple Shell Script in Linux

Now, getting started with the “vim” text editor, open the terminal and create a bash file using the command:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837246 834 How to Create a Simple Shell Script in Linux

When you executed this command, the editor will open like the image shown below with the file name “testing.sh” at the left bottom:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837247 41 How to Create a Simple Shell Script in Linux

Press “i” to get the insert mode and write a script in it:

#! /bin/bash
echo “Welcome to the Linux family.”

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837247 135 How to Create a Simple Shell Script in Linux

To save it, press “Esc” to exit the insert mode and type “:w” to save the script:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837248 186 How to Create a Simple Shell Script in Linux

The saved shell script will look like this:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837248 270 How to Create a Simple Shell Script in Linux

Type “:q” getting back to the console and execute the shell file to show output in the terminal:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837249 82 How to Create a Simple Shell Script in Linux

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.”

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837249 57 How to Create a Simple Shell Script in Linux

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

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837249 367 How to Create a Simple Shell Script in Linux

Execute the command to get the variable value as an output:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837250 44 How to Create a Simple Shell Script in Linux

Let’s do another example of for loop to print the numbers:

#! /bin/bash
for i in {1..7}
do
   echo $i
done

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837250 957 How to Create a Simple Shell Script in Linux

Now, give access to the file to execute:

$ chmod +x script_example.sh

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837251 877 How to Create a Simple Shell Script in Linux

Run the command to display output in the terminal:

how to create a simple shell script in linux How to Create a Simple Shell Script in Linux 1620837251 533 How to Create a Simple Shell Script in Linux

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.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More