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.
- 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”.
The default text editor will be open.
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"
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”.
Now, the script “tutorial.sh” will look something like this in the text editor after being saved.
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”.
Run the script using the following command:
The script name is “tutorial.sh” as mentioned above.
On running the above command, it will execute each line of script one by one and display the following output:
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.