What is the Bash Shebang and How to Use it

What is the Bash Shebang and How to Use it

This tutorial will explain what Bash Shebang is and how to use the Shebang characters in Bash scripts on the Linux system.

Shebang is a sequence of two characters: a number sign (#) and an exclamation mark (!) that gives us the #! at the beginning of every script we need to write. Shebang is also known as hashbang, pound-bang, or hash-pling. It is always defined in the first line of the script and is ignored by the interpreter.

In the next paragraphs, we will show you how to use the Shebang with real examples. Let’s get started!

Prerequisites

  • A server running any Linux OS
  • User privileges: root or non-root user with sudo privileges

1. Update the system

Before doing anything on the server, we must update its packages to the latest available version. In this blog post, we will use the latest version of Ubuntu 24.04 OS.

sudo apt update -y && sudo apt upgrade -y

2. How to use Shebang

As we already mentioned the Shebang #! is used on the first line of the script. It is threatened as a simple comment if used on the second, third, or other line. The Shebang directive has the following format:

#!interpreter [options]

For example, to use the bash shell interpreter, we need to define the following line:

#!/bin/bash

In this case, Shebang tells the system to use the Bourne sh shell to run the script.

3. Using Shebang in Bash Scripts

The most common usage of the Shebang is in the bash scripts. Including an appropriate shebang at the beginning of the script ensures that the desired shell interprets the script correctly. We can use two different methods to call the interpreter. The first method we already mentioned before:

#!/bin/bash

The second method is:

#!/usr/bin/env bash

These lines instruct the OS to run the script using the Bash interpreter at /bin/bash. Let’s create a simple bash script using the bash interpreter:

touch helloword.sh

Open the script with your favorite editor and paste the following lines of code:

#!/bin/bash
echo "Hello, world."

Save the file, close it, and make it executable with the command below:

chmod +x helloword.sh

Once done, execute the script:

sh helloword.sh

You should receive the following output:

root@host:~# sh helloword.sh
Hello, world.

Now, let’s create the script using the bash interpreter through env. You can remove and recreate the helloword.sh script or replace the lines of code with the following lines:

#!/usr/bin/env bash
echo "Hello, world."

After executing the script, you should get the same output:

root@host:~# sh helloword.sh
Hello, world.

Calling the bash interpreter using the second method is more flexible since the Bash interpreter can be installed in different locations.

4. Using Shebang in Python Scripts

We can specify the Python interpreter in the Shebang line to allow Bash script commands to be executed by Python script.

touch pythonScript.py

Need a fast and easy fix?
✔ Unlimited Managed Support
✔ Supports Your Software
✔ 2 CPU Cores
✔ 2 GB RAM
✔ 50 GB PCIe4 NVMe Disk
✔ 1854 GeekBench Score
✔ Unmetered Data Transfer
NVME 2 VPS

Now just $43 .99
/mo

GET YOUR VPS

Open the file with your favorite editor and paste the following lines of code:

#!/usr/bin/env python3
print('Python is not just a snake. It is a programming language!')

Save the file, close it, and make it executable:

chmod +x pythonScript.py

The /usr/bin/env locates the Python interpreter, and the OS uses it to execute the script. Now execute the script:

./pythonScript.py

You should receive the following output:

root@host:~# ./pythonScript.py
Python is not just a snake. It is a programming language!

5. Using Shebang For Other Interpreters

Shebang allows us to specify other interpreters than the bash or python. The other interpreters are Korn shell, Perl, shell as a configuration file, and many other custom interpreters.

#!/bin/ksh

#!/usr/bin/env perl

#!/bin/false

That’s it! You were introduced in the Bash scripts and the most important line the Shebang. If you have other questions about the Shebang and how to use it you can contact our technical support. We are available 24/7 and will be glad to help you!

PS. If you liked this post about Shebang and its usage, please share it with your friends on social networks or leave a comment in the comments section. Thank you.

Leave a Comment