How to Create and Run a Shell Script in Ubuntu 22.04


Suppose a system admin sends a greeting email to the user every day. Instead of doing it manually by executing every single command on the terminal, he or she can create a shell script and run it. In other words, a script is a batch or collection of commands that can be run simultaneously.

In today’s tutorial, I am going to show you the method of creating a Shell script in Ubuntu 22.04.

Make sure you have sudo or root privileges and the mail command is already installed on your machine. If not installed, run the following.

sudo apt install mailutils -y

Create a Shell script

Open your favorite editor. In my case, it is built-in Nano editor.

Type the following commands.

#!/bin/bash

Recipient=”[email protected]”

Subject=”Greeting”

Message=”Welcome to our site”

`mail -s $Subject $Recipient <<< $Message`

Create-Run-Shell-Script-Ubuntu-22-04

Save the file with .sh extension and exit the editor.

Run a Shell script

Open a terminal and execute the following command.

bash ./mail_example.sh

Create-Run-Shell-Script-Ubuntu-22-04

We have shown you how to create and (manually) run this script. The same script can also be scheduled to run automatically via cron jobs.

If you have any comments or queries, you are always welcome.

Cheers!

Print Friendly, PDF & Email
Categories