How to create and run a shell script in Ubuntu 22.04

Ubuntu Shell (Bash) Script

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’ve tested it on Ubuntu 22.04 and Ubuntu 20.04. Before demonstrating how to execute a shell script through CLI, we will first see how to create a shell script.

Prerequisites

  • Ubuntu system, any recent version will work.
  • 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 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”.

Open Text editor

The default text editor will be open.

Ubuntu Desktop Editor

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 echo commands/code in the untitled document.

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

Test script

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 – the home folder – and the file is named “tutorial.sh”.

Save test script

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

Shell script

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”.

Make the shell script executable

Run the script using the following command:

$ ./scriptname

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

Execute the shell script

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

Test script result

Conclusion

This article demonstrates the basic initials of shell script, and 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.