Clear, practical technology insights BSOD Code Lookup · Windows Error Code Lookup · Wi-Fi Troubleshooting · PC Troubleshooting Checklist

How to Create and Run Shell Scripts in Ubuntu 22.04: Step-by-Step Guide

Learn how to create and run shell scripts in Ubuntu 22.04. Follow clear steps, practical tips, and troubleshooting guidance to complete the process correctly.

Table of Contents

A script is used in Linux and commands are written into it according to job and task characteristics. When executing such a script, each command in it will be executed one by one in order. Shell is a user-written command interpreter. Script shell helps users write and execute multiple commands at the same time.

Key Takeaways About Run Shell Script in Ubuntu

  • A script is used in Linux and commands are written into it according to job and task characteristics.
  • When executing such a script, each command in it will be executed one by one in order.
  • Shell is a user-written command interpreter.

In this article, readers will learn how to execute shell scripts through command line input. This article is using Ubuntu 22.04 and Ubuntu 20.04. Before demonstrating how to execute a shell script via CLI, let's first look at how to create a shell script.

Prerequisites

  • Ubuntu system, any recent version will work.
  • The user has sudo privileges

Create a File with the Extension ".Sh"

To create a '.sh' file, follow these steps:

1. Open the Default Editor via the Menu Bar

You can use the default text editor in Ubuntu or if there is any other editor installed in your system such as vim editor. This particular tutorial is using the default text editor.

Type 'editor' and click 'Text Editor'.

How to create and run shell scripts in Ubuntu 22.04 Picture 1 - Run Shell Script in Ubuntu

The default editor will be opened.

How to create and run shell scripts in Ubuntu 22.04 Picture 2 - Run Shell Script in Ubuntu

2. Write the Command in an Untitled Document and Save It with ".Sh" Extension

You can write any command according to job requirements or assigned tasks. This particular article is writing the following echo commands/code in an untitled document.

echo "Hello World" echo "Ubuntu 20.04 LTS tutorial" echo "Today's task"

How to create and run shell scripts in Ubuntu 22.04 Picture 3 - Run Shell Script in Ubuntu

Now, save the file with the ".sh" extension to the folder of your choice. This example saves it to the default directory - the home directory - and the file is named "tutorial.sh".

How to create and run shell scripts in Ubuntu 22.04 Picture 4 - Run Shell Script in Ubuntu

Now the "tutorial.sh" script will look like this in the text editor after being saved.

How to create and run shell scripts in Ubuntu 22.04 Picture 5 - Run Shell Script in Ubuntu

3. Execute Shell Script 'Guide.sh' via CLI

You must execute the shell script via command line input.

You must first 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 tutorial, the script name is 'tutorial.sh'.

How to create and run shell scripts in Ubuntu 22.04 Picture 6 - Run Shell Script in Ubuntu

Run the script with the following command:

$ ./scriptname

The script name is 'tutorial.sh' as ??mentioned above.

How to create and run shell scripts in Ubuntu 22.04 Picture 7 - Run Shell Script in Ubuntu

When you run the above command, it will execute the script line by line and display the following output:

How to create and run shell scripts in Ubuntu 22.04 Picture 8 - Run Shell Script in Ubuntu

FAQ

How do you create and run shell scripts in Ubuntu 22.04?

A script is used in Linux and commands are written into it according to job and task characteristics.

What should you check before working with Run Shell Script in Ubuntu?

When executing such a script, each command in it will be executed one by one in order.

What can you do if the first method does not work?

Shell is a user-written command interpreter.

Discussion

Reader Comments 0

Sign in with email or Google to join the discussion.