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.

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

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.

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

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

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

3.6 ★ | 7 Vote

May be interested

  • How to Create Your Own Linux Distribution Using YoctoPhoto of How to Create Your Own Linux Distribution Using Yocto
    did you know that you can create your own linux distro with a little coding experience? building your own distro has the advantage that you can customize every aspect to suit your specific needs.
  • The reason and how to edit sudoers file in LinuxPhoto of The reason and how to edit sudoers file in Linux
    in a linux or macos system, there is a file called 'sudoers' that controls the deepest levels of your system permissions. it allows or denies a user to gain superuser access and holds some special options for sudo.
  • How to use sudo without password in LinuxPhoto of How to use sudo without password in Linux
    if you're the only one using your linux computer, why let it keep asking for passwords? let's see how you can use sudo without having to enter your password every time through the following article.
  • How to fix 'No Installation Candidate' error in UbuntuPhoto of How to fix 'No Installation Candidate' error in Ubuntu
    have you tried to install something, but ubuntu can't handle it? apt mentions something related to no installation candidate what does this mean, where does the source of the problem come from, and is it fixable?
  • How to enable Dark Mode in Ubuntu 20.04 LTSPhoto of How to enable Dark Mode in Ubuntu 20.04 LTS
    if you have ever used dark mode on a mobile phone, then you will be surprised to learn that it is also available on linux computers. in this tutorial, tipsmake.com will show you how to enable dark mode on a full ubuntu linux system.
  • How to install Hyper Terminal in Ubuntu 20.04Photo of How to install Hyper Terminal in Ubuntu 20.04
    hyper is an open source application supported on all three major platforms: windows, linux, and macos. in this tutorial, tipsmake.com will explain how to install hyper terminal in ubuntu system.