CentOS

How to Run a Bash Script in Ubuntu 22.04?

How to Run a Bash Script in Ubuntu 22.04

A Bash (Bourne Again Shell) Script is a text file possessing a series of commands delivering instructions to a bash shell. Bash scripts are designed to run on the Command Line. Bash scripts are widely used to automate tasks, for example, update/upgrade a system, backup work, create log files., etc.

A bash script can be executed in different methods. This article will demonstrate different ways of running a bash script in Ubuntu 22.04 LTS. This article will discuss:

  • How to Create a Bash Script in Ubuntu 22.04.
  • How to Run/Execute a Bash Script via Bash in Ubuntu 22.04?
  • How to Run/Execute a Bash Script via sh in Ubuntu 22.04?
  • How to Run/Execute Bash Script via Source in Ubuntu 22.04?
  • How to Run/Execute a Bash Script by Specifying the Absolute Path in Ubuntu 22.04?
  • How to Run/Execute Bash Script from Anywhere in Ubuntu 22.04?

How to Create a Bash Script in Ubuntu 22.04?

To create a bash script, first launch the terminal by pressing a combination of keys [Ctrl + Alt + T]. Next, a user can use a text editor, for example, Nano text editor to type the contents of the bash script. The following command is executed to create a bash script using Nano text editor

$ nano hello.sh

After the file is launched via Nano text editor, the contents of the file are typed in the file:

#!/bin/bash

echo "Hello World"

echo "This is a Test File"

Where,

  • #!/bin/bash: shell interpreter that will be used, i.e., bash. “#!” indicates the start of the script and is called shebang.
  • echo “Hello World” and echo “This is a Test File”: echo is used to print on the screen.


The file is then saved and closed by pressing Ctrl+O and Ctrl+X respectively. “ls” can be used to verify if the file is successfully created:

The above image shows that the script “testfile.sh” is successfully created. In the following sections, different ways of running a bash script will be discussed.

How to Run/Execute a Bash Script via Bash in Ubuntu 22.04?

Bash (Bourne Again Shell) is a shell interpreter. It is a replacement for sh (Bourne Shell) and includes features from different shells in Linux, for example, C shell and Kom shell. Bash allows you to recall earlier used commands by arrow keys. The bash interpreter resides in the /bin/bash. A bash script can be executed using bash by

$ bash testfile.sh

From the above image, it can be observed that the bash script is successfully executed as it displays the script contents.

How to Run/Execute a Bash Script via sh in Ubuntu 22.04?

“sh (Bourne Shell)” is the first UNIX shell. The sh shell resides in /bin/sh and /sbin/sh. A bash script can be executed using sh by

$ sh testfile.sh

From the above image, it can be observed that the bash script is successfully executed as it displays the script content.

How to Run/Execute Bash Script via Source in Ubuntu 22.04?

“Source” is a built-in shell command in Linux that executes the specified file content in the current shell. A bash script can either be executed by source command by

$ source testfile.sh

Or

$ . testfile.sh

From the above image, it can be observed that the bash script is successfully executed as it displays the script content.

How to Run/Execute Bash Script by Specifying the AbsolutePath in Ubuntu 22.04?

In order to run a bash script by specifying the absolute path, first, the script’s permission is changed to be executable by utilizing the chmod command:

$ chmod +x testfile.sh

As seen from the above image, initially the script file permission was -rw-rw-r–, i.e., the file was not executable for the user(u), groups(g), and others(o) groups. After execution of chmod command, the script permissions are updated to -rwxrwxr-x, i.e., it is not executable for all three groups.

More information about permissions and ownership in Ubuntu can be checked from the articles: How to Change File Permissions and Ownership in Linux/Ubuntu? and How does Chmod 777 Work in Linux/Ubuntu? The executable script can be executed by:

$ /home/linuxuser/Downloads/testfile.sh

From the above image, it can be observed that the bash script is successfully executed as it displays the script content. Additionally, the shortcut for home is represented by a tilde(~) character, therefore, the above command can also be typed as

$ ~/Downloads/testfile.sh

How to Run/Execute Bash Script from Anywhere in Ubuntu 22.04?

To execute a bash script from anywhere, the directory that contains the script is added to a $PATH by executing the following command

$ export PATH="/home/linuxuser/Downloads:$PATH"

After the directory is added to the $PATH, the script can be executed without specifying the path as follows:

$ testfile.sh

From the above image, it can be observed that the bash script is successfully executed as it displays the script content.

These $PATH changes are temporary and will no longer exist after the terminal is restarted. In order to add a directory to a $PATH permanently, either of the three files: .bashrc file, .profile file, and /etc/environment file can be modified. The details of permanently adding a directory to a $PATH can be seen in the article: How to Add a Directory to a $PATH in Linux/Ubuntu?

Conclusion

Bash Scripts are executed in Ubuntu 22.04 by bash, sh, source by bash <script>, sh <script>, and, source <script> commands respectively. Additionally, a script can be executed from anywhere either by specifying the absolute path or by adding the directory that contains the script to a $PATH. This article demonstrated different ways of running a bash script in Ubuntu 22.04 LTS.

Similar Posts