“#!” Shebang (Sha-bang, Hashbang)

I came across this line #!/usr/bin/env and was wondering what is this and for what? I browsed some articles to get some overview about it. Any script beginning with #! is called shebang(other names are hashbang, sha-bang etc). When a script with shebang is run, it passes the file as an argument to the specified program instead of trying to execute it.

The form of a shebang interpreter directive is as follows:

#! interpreter [optional-arg]

The interpreter must be an absolute path to an executable

In order to run the script, we need to tell the shell three things:

  1. That the file is a script –>accomplished using shebang #!

  2. Which interpreter we want to execute the script

  3. The path of said interpreter

2 and 3 is accomplished using env. 

Some typical shebang lines:

  • #!/bin/sh — Execute the file using sh
  • #!/usr/bin/perl — Execute using Perl
  • #!/usr/bin/python — Execute using Python

Shebang lines may include specific options that are passed to the interpreter.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s