How to Create and Run a Shell Script on Ubuntu 20.04

Wondering how to create and run a shell script on Ubuntu 20.04 LTS? Don’t worry, we got you covered!

Command Terminals allow their users to perform their tasks by typing and running a few lines of commands. While it has its uses, there are cases where you might need to repeat certain processes.

In cases like these, retyping the same commands over and over can prove to be a hassle. This is where shell scripts come in.

Shell scripts are files that hold a sequence of commands. Upon running a shell script, the script’s contents are executed in the order they’re written. These scripts can be executed like any other command in the Terminal.

If you’re looking to learn how to create and run a shell script on Ubuntu 20.04 LTS, then you’ve come to the right place. We’ll go over the methods to create a shell script and multiple ways to run them from the Command Terminal.

So without further ado, let’s begin!


Creating a Shell Script – Method 1: Using the CLI.

Creating a shell script in Ubuntu is a simple process and can be done by using both CLI and GUI methods.

The CLI method uses the nano editor and allows you to create and save the file from the comfort of your Terminal window.

It should be noted that we’re using Ubuntu 20.04 LTS with root privileges. However, this method can be used for other versions of Ubuntu as well.

Follow these steps to create a shell script on Ubuntu:

  1. Start by opening the Command Terminal. The shortcut is Ctrl + Alt + T.
  2. Next, type out the following command in the Terminal:
$ sudo nano <file name>.sh
run a shell script on ubuntu

This will open a text file in the nano text editor. You should see a screen as shown in the image below.

testfile
  1. Shell scripts exist in the “.sh” format. To ensure that your text file is saved as a shell script, type the following at the beginning of the file.
#!/bin/bash

Or 

#!/bin/sh

This allows the file to be interpreted as a bash or shell script.

  1. Afterward, type the commands that you wish to execute in the script. Take note that the commands will be executed sequentially.
GNU nano
  1. Lastly, save the file by pressing Ctrl + X. The editor will prompt you to save the file. Type Y and press Enter.

By now, you should now have a shell script created on your system successfully. If that isn’t the case, then worry not, you can try out the GUI method mentioned below.


Creating a Shell Script – Method 2: Using the GUI.

If the previous method seemed hard to follow, then you can try out the GUI method.

This method makes use of the default GUI-based text editor.

Follow these steps to create a shell script.

  1. Start by clicking on the “Show Applications” button.
show applications
  1. Next, navigate to the search bar and type “text editor”.
text editor
  1. Click on the first result. This should open a file, as shown in the image below
run a shell script on ubuntu
  1. In the text file, type the commands that you wish to execute as a shell script.
untitled document
  1. Once you’ve written all the commands, proceed to save the file in your desired location. Remember to name the file as <filename>.sh to ensure it gets saved as a shell script.
run a shell script on ubuntu

If you correctly followed the steps, you should now have created a shell script in Ubuntu. All that’s left to do is to learn how to run it.


Running a Shell Script.

While running a shell script on Ubuntu 20.04 uses the Command Terminal, there are multiple methods to achieve this. We’ll cover all relevant methods to ensure that you’re ready to run shell scripts in more ways than one.

The first method to run a shell script on Ubuntu uses the $chmod command. This is also the most common method to run a shell script.

Follow these steps to run a shell script using the $chmod command:

  1. First, open the Command Terminal. The shortcut key is Ctrl + Alt + T.
  2. Once it is open, type the following command:
$ sudo chmod +x <filename>.sh
command line

Upon execution, this command will grant your shell script execution privileges.

  1. Lastly, type the following to execute the script:
$ ./<file name>.sh

If you followed the steps correctly, then the script should have been executed successfully. In case this method doesn’t work out, keep reading.

Another method that you can use to run a shell script on Ubuntu is by using the bash command. To run the shell script, follow these steps:

  1. Start by opening the Command Terminal. The shortcut is Ctrl + Alt + T.
  2. Once opened, type the following command:
$ sudo bash <file name>.sh
run a shell script on ubuntu

This should display the output of the commands in the script.

run a shell script on ubuntu

If the previous methods don’t work out, you can try to run the shell script with the help of the sh command. To run the shell script using the sh command, follow these steps:

  1. First, open the Command Terminal. The shortcut key is Ctrl + Alt + T.
  2. Next, type in the following command:
$ sudo sh <file name>.sh

Upon running this command, you should see the script output in the Terminal window.

run a shell script on ubuntu

While shell scripts can make your life easier by allowing you to execute multiple commands, the process of running them is also prone to the problem of command repetition.

You might find yourself repeating the shell script command in case you need to execute the script again and again. Fortunately, there’s a way around this. You can avoid writing the long command by replacing it with an Alias. Learning how to make an alias can save you time in the long run.

This ends our guide on how to create and run a shell script on Ubuntu 20.04 LTS. We covered the methods of creating a shell script, along with methods of running it. If you have any queries or suggestions, let us know in the comment section down below.

If this guide helped you, please share it. 🙂

Leave a Reply

Your email address will not be published.

Previous Post

Ways to Reboot Ubuntu Through the Command Line

Next Post

How to Fix Excel Sheet Tabs Hidden Behind Taskbar

Related Posts