Why we use #!/bin/bash (shebang) in scripting

3mp3r0r
2 min readDec 6, 2020

--

I have been familiar with reading scripts for a some time now and started my bash scripting after watching Hackersploit’s playlist. But I could never understand properly why we use a shebang at the top of scripts. Thanks to Heath Adams he explained it in a video from one of his courses which I was watching yesterday.

I know its a funny word but it is what it is lol.

Following is a very basic python program which i will use here.

Red line is called the ‘shebang’

Using shebang is not mandatory and depends on how we run the program. There are two ways we can execute this python file from CLI, one requires use of shebang and the other does not.

First way — requires shebang

+x for giving it executable permission

First we make the file executable and then execute it using prefix ./ without explicitly mentioning the use of ‘python’ like we will do in second method. Since we do not mention what interpreter to use, shebang tells that this program will run using a specific interpreter (which is python here).

Second way — does not requires a shebang

In this, we explicitly mention the ‘python’ interpreter to be used to run myscript.py. This method would work even if we do not use the shebang.

Hope you learned something useful :)

--

--