How to Create a Shell Script in Linux
Do you want to create a Shell script in your Linux system?
This guide will take you through how to create a shell script using multiple text editors, how to add comments, and how to use Shell variables.
But before heading over to creating a shell script, let’s understand what Shell scripting in Linux is.
What is Shell Scripting in Linux?
Shell Scripting is defined as an open-source program that’s run by Linux or Unix shell. Through shell scripting, you can write commands to be executed by the shell.
Lengthy and repetitive commands are usually combined into a simple command script. You can store this script and execute it whenever needed.
Shell scripting in Linux makes programming effortless.
Ways of Creating a Simple Shell Script in Linux
Creating a simple shell script in Linux is very easy. You can do that using multiple text editors. This tutorial will show how to create a shell script with two different methods, such as 1) using the default text editor, and 2) Using the Vim text editor tool.
Method 1: Using the Default Text Editor
To create a shell script using the default text editor, just follow the steps given below.
Step 1: Create a text file having a “.sh” extension. Then type a simple script.
Step 2: Now don’t change the directory. And open the terminal. Using the command below, give executable access to the file created.
Step 3: Execute the below-given script in the terminal:
This was a simple technique of creating a shell script in Linux using the default editor. Now, let’s look at the next method.
Method 2: Using the Vim Text Editor Tool
Vim text editor tool is a tool that helps create a shell script in a Linux system. In case you don’t have it already installed, use the command to install Vim:
Now follow the steps for creating a shell script using the tool.
Step 1: For opening the editor, simply type:
Step 2: Once you’re in, open the terminal. Then create a bash file via:
After the execution of the command, the editor will appear as below.
Step 3: Press “i” from the keyboard and get into the Insert mode. Now, type the following commands in it.
#! /bin/bash echo “Welcome to the Linux family.”
Step 4: Press “Esc” to exit this mode. Then type “:w” to save your script.
Once saved, the shell script will appear as below.
Step 5: When you’re back to the console, type “:q”. Then write:
The above command will execute the shell file and will display the output in the terminal.
Adding Comments in Shell
Just like in many other programming languages, we use “#” (the hashtag symbol) to add comments in the shell script.
In the Vim editor, open the text file. And write a comment as below.
#! /bin/bash # This is the testing script! echo “Welcome to the Linux family.”
The above example illustrates the usage of the “#” symbol in adding comments in a shell script.
Using Shell Variables
Variables are a value that can store information in the form of numbers and characters. Likewise, Shell variables can store data as well. For example,
The above lines will create a shell variable “var” and then will print it.
Let’s write a small script, now.
#! /bin/bash variable=“This is the testing shell script.” echo $variable
To get the value of the variable, execute the command below.
Let’s write another script using a variable.
#!/bin/sh echo "what is your name?" read name echo "How do you do, $name?" read remark echo "I am $remark too!"
Run the script file and enter the “name” as “Robin”.
bash scriptsample.sh what is your name? Robin
As you enter the input, the script reads the name and replies:
Then enter the “remark” as “good”. And you’ll notice that the script repeats the remark.
The second line of the above set of codes shows the response from the script.
These were some simple shell scripts. You can write advanced scripts containing loops, functions, and conditional statements. Shell scripting will make Linux administration a breeze.
The Conclusion
In this article, you have learned to create a simple Shell script. Now you can create your own scripts in Linux following the above-mentioned methods and tips.
Suparna is a freelance writer who writes about Linux including tips, tricks, and how-tos.