How to create and run a shell script in Ubuntu 20.04 LTS

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.

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.

Picture 1 of How to create and run a shell script in Ubuntu 20.04 LTS

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.

Picture 2 of How to create and run a shell script in Ubuntu 20.04 LTS
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'.

Picture 3 of How to create and run a shell script in Ubuntu 20.04 LTS
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:

Picture 4 of How to create and run a shell script in Ubuntu 20.04 LTS

Update 18 October 2020
Category

System

Mac OS X

Hardware

Game

Tech info

Technology

Science

Life

Application

Electric

Program

Mobile