Using Shebang in Linux (original) (raw)

Last Updated : 23 Apr, 2026

The shebang is an interpreter directive used in Unix-like operating systems. It is a character sequence, #!, that constitutes the first line of a script. This line specifies the absolute path to the system interpreter (such as bash, python3, or perl) required to execute the commands within the script. You will use a shebang to:

Usage of the Shebang

In some Linux programs that are written in scripting languages like Python, Perl, and Shell-script, you will see a line right at the top that starts with #!/ like this one:

shebang-example

Components of the Shebang

There are three distinct concepts that work together.

1. The Shebang (#!)

The name "shebang" comes from its two characters:

When you try to run a file, the Linux kernel opens it, sees the #! at the beginning, and knows it shouldn't run the file itself. Instead, it reads the rest of the line as a command to run, using the script as the input for that command.

2. The Path

The path is the part after the #!.

#!/usr/bin/python3

3. The Executable Permission (chmod +x)

The shebang tells the system how to run the script, but the file's permissions tell the system if it's allowed to be run.

This is a separate and mandatory step.

Understanding Executable Files

We will understand how files are converted into executables in Windows and Linux.

**On Windows:

If you've used Windows before, you're likely familiar with executable files, commonly recognized by their .exe extension. These files are designed to launch programs and can be run in two simple ways:

**On Linux:

When you transition to Linux, you'll notice a key difference: there's no fixed file extension for executables. Instead, Linux relies on file permissions to determine executability. Here's how it works:

./filename

Screenshot-from-2024-01-10-17-09-28

echo Hello, World!

chmod +x

A Python Example

Suppose we wish to make an executable in Python. We can write a Python script which will input a number and return its square. We want to run this executable. If we have Python installed, we can run the file simply by using the following command:

python

sample

**Question: But suppose we want to run this program without using the Python command? In other words, can we run the Python script square.py only using ./square.py and without using the python command?

Yes, you can, and that's where the Shebang comes into play.

Using a Shebang in Linux

You can use a shebang to tell Linux which interpreter is needed to run the script. For instance, in the case of a Python file, you will need the python executable. Usually, this is located in /usr/bin. You will need the complete path to the executable.

which

Screenshot-from-2024-01-12-12-12-43

#!

#!/usr/bin/python

#!/usr/bin/python
number = int(input())
print(number * number)

chmod +x square.py

shebanged

**Note: The Shebang does not make your file executable by itself. It simply tells Bash (the shell) where to find the interpreter to run it. You will still need to have the interpreter installed.

Using env

When you write a script with a shebang (#!), it might not work on another machine because different systems can have interpreters (like Python or Node.js) installed in different locations. To make your shebangs portable across all systems, you can use the env command.

/usr/bin/env

For Python:
#!/usr/bin/env python3

For Node-JS (if the script is in Javascript):
#!/usr/bin/env node

For Perl:
#!/usr/bin/env perl

Executable Path and Shell Commands

When you use an executable script in Linux with a shebang, you can make it become a shell command! To do this, you need to place it in a PATH location. A PATH location is a special folder in your Linux filesystem, and all executables inside a PATH location can be used like commands in Linux.

There are two ways to do this:

  1. You can look up the existing PATH locations, find one that is to your liking, and place your executable script file there. Remember to set the executable flag with chmod +x after you move the file there.
  2. You can add the folder in which your script file is located to the PATH variable, and make the file a command.

Below, we will discuss both of these methods.

Adding Your File to a PATH Location

echo $PATH | sed "s/:/\n/g"

Screenshot-from-2024-01-12-14-46-51

/home/user/.local/bin

using

Adding a Folder to the PATH List

In general, it is a good idea to make yourself a new directory, add all your executable script files there, and then add that directory to the PATH locations. This is especially useful if you do not already have a folder named bin inside ~/.local directory.

1. First, make the folder which you need to add to PATH.

2. Second, not the path address of the folder. For instance, if the folder is named mypath and it is inside my home directory, then the location will be

/home/user/mypath

3. Third, open the .bashrc file in a preferred editor. This is a hidden file inside the home folder. You can see it in your GUI by using Ctrl+H to view hidden files, and then double-click on it. Alternatively, inside a terminal, you can use nano, vi, or vim to open the file with a command:

nano ~/.bashrc

export PATH="$PATH:"

export PATH="$PATH:/home/user/mypath"

4. The last step is to close and reopen your terminal.

All terminals you open from this point on will treat executable files inside mypath as commands. If you do not want to close and reopen your terminal, simply run the command

. ~/.bashrc

**Remember: Your script must have a correct Shebang for it to become a command!

Some Other Uses

Shebangs allow you to choose the interpreter which runs a script, so that the user does not have to select the interpreter at run time. This can be used to turn a script into a command, but it also has some other uses. We are going to go over some of them.

1. Ensuring Compatibility

Different Linux and UNIX distributions use different default shells, which can cause scripts to behave inconsistently across systems.

Hence, we can ensure that our script runs identically on all of these systems by writing a script for the Bourne Shell using this shebang:

#!/bin/bash

2. Specifying Interpreter Version

Different versions of the same shell, such as Bash, can have varying features and security vulnerabilities. Older enterprise systems often run outdated shells, which may expose them to risks.

For instance, to ensure that the script runs with Bash 5.2, they can use:

#!/bin/bash-5.2

#!/bin/bash-latest

3. Preventing Direct Execution

To disable a script from being run in the shell by a user who didn't read your README file, just use this shebang:

#!/usr/bin/false

You can still run a script with such a shebang using the **source keyword, but it cannot be run directly as an executable.

Possible Malicious Use

In Linux, a shebang (#!) defines which interpreter should execute a script. While it’s a useful feature, it can also be misused to run malicious executables, making it important to verify before executing unknown scripts.