How to create and run a shell script in Ubuntu 20.04 LTS
A script is used in Linux and commands are written into it according to job and task characteristics. When executing such a script, each of the commands will execute one by one in order. Shell is the command interpreter written by the user. Script shell helps users write and execute multiple commands at the same time.
In this article, you will learn how to execute shell scripts through command line input. The article is using Ubuntu 20.04 LTS. Before showing how to execute shell scripts through the CLI, let's first look at how to create a shell script.
Create file with '.sh' extension
To create a '.sh' file, follow these steps:
1. Open the default editor through the menu bar
You can use the default editor in Ubuntu 20.04 LTS or if there is any other editor installed in your system, such as Vim. For this particular tutorial, the article is using the default editor.
Type 'editor' and click 'Text Editor' .
The default editor will be opened.
2. Write commands in document without title and save it with '.sh' extension
You can write any command according to job requirements or assigned tasks. For this particular article, the following commands / codes will be written in the document without the title.
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 choice. For this particular article, the file is saved in the default directory, which is the main directory named 'tutorial.sh'.
Now the script 'tutorial.sh' will look like this in the editor after it is saved.
Script 'tutorial.sh' in the editor
3. Execute the shell script 'tutorial.sh' through the CLI
You must execute the shell script via command line input.
First, you must make the shell script executable by running the following command:
$ ./scriptname
Replace your shell script name in the position of 'scriptname' in the above command. For this particular tutorial, the script name is 'tutorial.sh'.
Makes the shell script executable
Run the script with the following command:
$ ./scriptname
The script name is 'tutorial.sh' as mentioned above.
When running the above command, the script line by line will be executed and display the following output:
You should read it
- How to create and run shell scripts in Ubuntu 22.04
- How to change user accounts in Ubuntu Bash Shell Windows 10
- How to 'turn' Ubuntu into Windows
- Everything you can do with the new Windows 10 Bash Shell
- Use Google applications more efficiently with Google Apps Script
- Flow control in Shell
- What is Google Cloud Shell? What's remarkable about Cloud Shell?
- What is a shell?
- How to change the default shell in Windows Terminal
- How to use Open-Shell in Windows 11
- Managing Windows networks using Script - Part 13: The script returns all values
- How to Change Themes on Ubuntu with Gnome Tweak Tool
Maybe you are interested
How to execute Shell script in Linux
This is why people cancel their X Premium subscription!
Learn about Shockwave Unlimited: Subscription that replaces Game Pass and PS Plus
3 AI-powered video transcript creation tools to save hours of viewing
Why did YouTube cancel your YouTube Premium subscription?
Which Spotify subscription is best for you?