Shell Scripting – Understanding Shebang (#!/bin/bash) (original) (raw)

Last Updated : 23 Feb, 2026

Scripts are a powerful way to automate tasks in Linux. A shell provides an interface between the user and the operating system. When working in a Linux terminal, users interact with the system through the shell to execute commands. In shell scripting, the shebang (#!/bin/bash) specifies which interpreter should run a script.

Shebang

The shebang is a special character sequence placed at the beginning of a script file. It starts with #! and is followed by the path of the interpreter that should run the script. When a script is executed, the operating system reads the shebang line and uses the specified program to interpret the script.

Purpose of Shebang

The shebang ensures that scripts:

**Note: Without a shebang, the system may not know how to interpret the script correctly.

Format of Shebang

#!/path/to/interpreter

**Example:

#!/bin/bash

Example 1: Using #!/bin/bash in a Script

To show how a shebang specifies Bash as the interpreter for a script.

**Script (File: hello.sh)

#!/bin/bash
echo "Hello World!"

Hello-script

**Command:

chmod +x hello.sh
./hello.sh

**Output:

Hello-script-op

Example 2: Running a Script Without Shebang

To show what happens when a script does not contain a shebang.

**Script (File: test.sh)

echo "This is a test"

test

**Command:

chmod +x test.sh
./test.sh

**Note: If the default shell is not compatible, the script may fail.

**Output (May Vary):

test-op

Example 3: Using a Different Interpreter in Shebang

To show how shebang can be used with other shells.

**Script (File: sh_script.sh)

#!/bin/sh
echo "Running with sh"

sh-script

**Command:

chmod +x sh_script.sh
./sh_script.sh

**Output:

sh-script-op

Syntax of Shebang (#!/bin/bash)

The shebang follows a fixed format that must be written correctly for the script to execute properly.

#!/path/to/interpreter [optional-arguments]

**Note: Without #!, the system will not treat the file as an executable script.

**Example:

/bin/bash

**Note: The path must be correct. If the interpreter does not exist at that location, the script will fail.

Important Rules for Shebang

Difference between #!/bin/bash and #!/bin/sh:

#!/bin/bash

#!/bin/sh

Other Common Shebangs

**1. #!/bin/csh

**2. #!/usr/bin/perl -T

**3. #!/usr/bin/php

**4. #!/usr/bin/python -O

**5. #!/usr/bin/ruby

Also Check: