Igor Šarčević

Shebang

Unix systems are really smart when it comes to interpreting your application. Not only do they provide you with an excellent environment for development, but also let you specify the interpreter in you source file. As it turns out, this technique lets you write an application in any programming language without forcing your end users to know the details of your implementation. This article is about describing this simple technique.

Lets start with an example hello world application written in bash script.

echo "Hello world!"

To execute such a program we must do two things. Make the program executable, and run it with the bash interpreter.

$ chmod +x hello_world.sh
$ bash hello_world.sh
Hello world!

Now, we can add the shebang to the start of the program.

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

and with that simple addition we can now invoke our program without the bash interpreter

$ ./hello_world.sh

Rules for the shebang

The shebang command must be the first line of the file and can contain any valid path for the interpreter, followed by an argument that the command will receive.

The shebang line is read by the system before the execution of the program, but that line will not be automatically deleted. So if you want to write your own interpreter, you must manually handle that line.

For example we can even write a program that outputs itself by writing

#!/bin/cat
Answer to the ultimate question is 42!

Better paths in the shebang

The above examples used absolute paths to address the commands in the shebang line. While this is ok, it would be a safer alternative to use the /usr/bin/env path like in the following example.

#!/usr/bin/env cat
Answer to the ultimate question is 42!

The above can help you mitigate problems when the command is not in its expected folder by using the paths set in your environment variables.

Summary

This is one of the easiest things you can learn to write better scripts, and it is invaluable if you never heard of it before.

Happy hacking!

Update: maandree pointed out that it is safer to use /usr/bin/env and that the shebang command takes only one argument