A script is used in Linux and has written commands into it according to work specifications and assignments. On executing such a script, each command in the script executes in order one by one. The shell is the user-written commands interpreter. A Shell script helps a user with writing and executing multiple commands at the same time.

In this article, we will see how to execute shell scripts through command line input. With reference to this particular article, I am using Ubuntu 20.04 LTS. Before demonstrating how to execute a shell script through CLI, we will first see how to create a shell script.

Prerequisites

  • Ubuntu 20.04 LTS system
  • User with sudo privileges

Create a file with “.sh” extension

To create a “.sh” file, follow the following steps:

1. Open default “Text Editor” through the menu bar

You can either use the default text editor in Ubuntu 20.04 LTS or if there is any other editor installed in your system for example “vim text editor”. For this particular guide, I am using the default text editor.

Type “editor” and click on the ”Text Editor”.

<img alt="Open Text editor" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1677.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="255" src="data:image/svg xml,” width=”797″>

The default text editor will be open.

<img alt="Ubuntu Desktop Editor" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1678.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="422" loading="lazy" src="data:image/svg xml,” width=”538″>

2. Write commands in the untitled document and save it with the extension “.sh”

You can write any commands as per your work requirements or assigned tasks. For this particular article, I am writing the following commands/code in the untitled document.

echo "Hello World"
echo "Ubuntu 20.04 LTS tutorial"
echo "Today’s task"

<img alt="Test script" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1679.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="159" loading="lazy" src="data:image/svg xml,” width=”540″>

Now save the file with the “.sh” extension in the directory of your own choice. For this particular article, I am saving it in the default folder that is the home folder named as “tutorial.sh”.

<img alt="Save test script" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1680.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="448" loading="lazy" src="data:image/svg xml,” width=”719″>

Now, the script “tutorial.sh” will look something like this in the text editor after being saved.Advertisement

<img alt="Shell script" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1681.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="184" loading="lazy" src="data:image/svg xml,” width=”547″>

3. Execute the shell script “tutorial.sh” through CLI

You have to execute the shell script through command line input.

First, you have to make the shell script executable by running the following command:

$ chmod  x scriptname

Write your shell script name in place of “scriptname” in the above command. For this particular guide, the script name is “tutorial.sh”.

<img alt="Make the shell script executable" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1682.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="56" loading="lazy" src="data:image/svg xml,” width=”719″>

Run the script using the following command:

$ ./scriptname

The script name is “tutorial.sh” as mentioned above.

<img alt="Execute the shell script" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1683.png" data-ez ezimgfmt="rs rscb10 src ng ngcb10 srcset" height="26" loading="lazy" src="data:image/svg xml,” width=”345″>

On running the above command, it will execute each line of script one by one and display the following output:

<img alt="Test script result" data-ezsrc="https://kirelos.com/wp-content/uploads/2022/04/echo/word-image-1684.png" ezimgfmt="rs rscb10 src ng ngcb10" height="96" loading="lazy" src="data:image/svg xml,” width=”249″>

Conclusion

This article demonstrates the basic initials of shell script, how to create and execute a shell script in a very simple and easy to understand manner. The user can perform each step by looking through the attached screenshots of each input command and what will be the correct output of that command. The article will help both professional and naive users equally.