While we believe that this content benefits our community, we have not yet thoroughly reviewed it. If you have any suggestions for improvements, please let us know by clicking the “report an issue“ button at the bottom of the tutorial.
Shell is a command-line interpreter that allows the user to interact with the system. It is responsible for taking inputs from the user and displaying the output.
Shell scripts are a series of commands written in order of execution. These scripts can contain functions, loops, commands, variables. Scripts make it easy for the users to save certain sequences of codes that might be used again and again. Shell scripts can also have comments to increase readability.
A shell script needs to be saved with the extension .sh.
To let the Linux system know that the file is a shell script, the file needs to begin with the shebang construct.
#!/bin/bash
OR
#!/bin/sh
After this, the script can contain commands, functions, loops, conditional checks, etc.
A good script always contains comments that make it readable.
A shell script can be created using vi, cat command, or the normal text editor in GUI.
Let’s create a basic shell script using vi
$ vi basic_script.sh
This will take you to the vi editor. Add the following lines:
#!/bin/bash
whoami
date
This simple script should display the current user followed by the date.
To save and exit the vi editor:
By default, the creator of the script does not get executable permission for the file.
To change that:
$ chmod +x basic_script.sh
This will give you (current user) the permission to execute the file.
To run the script :
$ bash basic_script.sh
The first line of output corresponds to ‘whoami’ command and the second line to ‘date’ command.
Another way of running the script is :
$ ./basic_script.sh
Running the file this way might require the user to give permission first. Running it with ‘bash’ doesn’t require the permission.
The same script is running with ‘bash’ before it, but having permission issues when tried to execute directly. The reason this is happening is that the command bash [filename] only needs read permission from the file.
Whereas the command ./[filename] run the file as an executable and hence requires the permission to execute. This question has been answered in detail on StackExchange.
In general it is better to provide executable permission.
Scripts can include user-defined variables, in fact as scripts get voluminous in size it is essential to have variables that are clearly defined. Variables that are self-descriptive in nature is another quality of a good script.
Add the following lines to the script :
#!/bin/bash
#This is a comment
#defining a variable
GREETINGS="Hello! How are you"
echo $GREETINGS
GREETINGS is the variable defined and later accessed using ‘$’.
There should be no space in the line where variables being assigned a value.
Let’s run the script:
Shell scripts can be made interactive with the ability to accept input from the command line. Read command can be used to store the command line input in a variable.
#!/bin/bash
#This is a comment
#defining a variable
echo "What is your name?"
#reading input
read NAME
#defining a variable
GREETINGS="Hello! How are you"
echo $NAME $GREETINGS
A variable NAME has been used to accept input from the command line.
On running the script :
Users can define their own functions in a script. These functions can take multiple arguments.
In the script add :
#!/bin/bash
#This is a comment
#defining a variable
echo "What is the name of the directory you want to create?"
#reading input
read NAME
#defining a variable
echo "Creating $NAME ..."
mkcd ()
{
mkdir "$NAME"
cd "$NAME"
}
mkcd
echo "You are now in $NAME"
This script will ask the user for a directory name. It will then create the directory and cd into it.
We saw how scripts can be used to run commands in a serial order. Scripts help users in reducing redundancy and save time. Scripts can also be shared among different users.
The Scripts we saw in this tutorial were quite basic, scripts can be designed to perform complex tasks as well. To learn more about scripting refer this.
Join our DigitalOcean community of over a million developers for free! Get help and share knowledge in our Questions & Answers section, find tutorials and tools that will help you grow as a developer and scale your project or business, and subscribe to topics of interest.
Sign up
Hello Jayant! Thanks for this post, is really useful for me. Is there any way to run a command with options within a shell script? Thanks again!
- Juan
I like it. Crisp and simple :)
- khushbu
I have gone through the blog.It is very good
- Siddharth Pandey