How to Execute a Command in a Shell Script?

Filed Under: UNIX/Linux
Execute A Command In Shell Scripts

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.

Creating and running a basic shell script

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:

  • Press ESC
  • Type :
  • Type in ‘wq’
  • Hit Enter

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
Basic Script 1

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.

Bash Permision

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.

Bash

In general it is better to provide executable permission.

Using variables in shell scripts

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
Shell Script In Vi

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:

Using Variable Output

Reading input from the command line

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 :

Reading Output

Defining functions

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.

Funciton In A Script 1

Conclusion

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.

Comments

  1. Siddharth Pandey says:

    I have gone through the blog.It is very good

    1. Jayant says:

      $ echo (” I am glad you found the blog valuable”)

Leave a Reply

Your email address will not be published. Required fields are marked *

close
Generic selectors
Exact matches only
Search in title
Search in content
Search in posts
Search in pages