Do you want to create a shell script in your Linux system?
This guide will show you how to create a shell script using several text editors, how to add comments, and how to use shell variables.
But before moving on to creating a shell script, let’s understand what shell scripting is in Linux.
What is Shell Scripting in Linux?
So what is the shell script?
Shell Scripting is defined as an open source program executed by Linux or Unix shell. With shell scripts, you can write commands for the shell to execute.
Long and repetitive commands are usually combined into a single command script. You can store this script and run it whenever needed.
Shell scripts in Linux make programming easier.
Ways to Create a Simple Shell Script in Linux
Creating a simple shell script in Linux is very simple. You can do this using several 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 editing tool.
Method 1: Using the Default Text Editor
To create a shell script using the default text editor, just follow the steps below.
Step 1: Create a text file with a “.sh” extension. Then type a simple script.
2nd step: Now don’t change directory. And open the terminal. Using the command below, give executable access to the created file.
chmod +x testing.sh
Step 3: Run the script below in the terminal:
This was a simple technique for creating a shell script in Linux using the default editor. Now let’s look at the next method.
Method 2: Using the Vim Text Editing Tool
The Vim text editing tool is a tool for creating a shell script in a Linux system. If you don’t already have it installed, use the command to install Vim:
sudo apt install vim
Now follow the steps for creating a shell script using the tool.
Step 1: To open the editor, just type:
2nd step: Once you’re in, open the terminal. Then create a bash file via:
After executing the command, the editor will appear as below.
Step 3: Press “i” on the keyboard and enter 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 return to the console, type “:q”. Then write:
The above command will run the shell file and show the output in the terminal.
Adding Comments in Shell
As 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 example above illustrates the use 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. Similarly, Shell variables can also store data. For example,
var ="Hello" echo $var
The above lines will create a “var” shell variable and then print it.
Let’s write a little script now.
#! /bin/bash variable=“This is the testing shell script.” echo $variable
To get the value of the variable, run 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
When you type the input, the script reads the name and responds:
How do you do, Robin?
Then enter the “remark” as “good”. And you will notice that the script repeats the remark.
good I am good, too!
The second line of the code set above shows the script’s response.
It was a few simple shell scripts. You can write advanced scripts containing loops, functions, and conditional statements. Shell scripts will make Linux administration a breeze.
In this article, you learned how to create a simple shell script. You can now create your own scripts in Linux by following the methods and tips mentioned above.