Home » Linux » Shebang #! in Linux Scripts

About Iulian Timis

Avatar photo
My name is Iulian and I graduated Faculty of Electronics, Telecommunications, and Information Technology. My master's degree was in Computer Science, Information Security. I am passionate about technology and the security area. During my career I was working as a Java Developer, on the backend and frontend side, sometimes doing some DevOps tasks.

Shebang #! in Linux Scripts

1. Introduction

When you write scripts in Linux you have to know the meaning of the first line from a bash script and why you need it. In a non-computer science environment phrase “the whole shebang” means “everything”.

In computer science, a shebang is a sequence of characters. The first character is a number sign (#) and the second is an exclamation mark (!). This is also known as sharp-exclamation, sha-bang, hashbang, pund-bang, or hash ping.

2. Why do we use the shebang?

The first line from a file is an executable in the Unix operating system. The program loader parser will know that the first line will be interpreted as a directive. The loader will execute the specified interpreter program set in the first shebang line and as a first parameter will pass the path of the initial script. That program may use the file as input data. So basically the shebang line is a way to declare what interpreter the operating system will use to parse the script file.

E.g: We have a script test.sh and the first line is #!/bin/sh then the program loader will know to run the program /bin/sh and pass the test.sh as a first argument.

The syntax of the shebang is:

#!<path to interpreter> [optional-arg]

For more information about the shebang concept, you can check out here.

3. Using shebang in a Shell Script

To use the shebang in your Linux scripts you have just to add it in the first line, the path to the interpreter, and an optional argument if you need it.

Fig.1: Usage of the shebang line in the shell script

4. Using /usr/bin/env program

The env from the shebang will instruct the system to look for the specified interpreter. It will look in the $PATH variable and get the first occurrence. The default location for Unix-based systems is: /usr/bin/env. To verify its location you can type: which env the returned output is: /usr/bin/env. The env path is in the same location on different operating systems.

Output of the env command
Fig.2: Output of the env command

As we can see on the below output the SHELL interpreter is located in the environment properties.

The advantage of using shebang line /usr/bin/env is that you can port your script across different UNIX-like operating systems. Different interpreters can be in different locations on different UNIX operating systems.

5. Conclusion

For Sysadmins, Linux developers, and other people that have interaction with Linux Scripts it is important knowing the meaning and the role of that line in order to have a deep knowledge of the script concepts.

In this article, we introduce the concept of shebang, the usage of that line, and some examples of how to use it in the shell scripts of other interpreters.

Do you want to know how to develop your skillset to become a Java Rockstar?

Subscribe to our newsletter to start Rocking right now!

To get you started we give you our best selling eBooks for FREE!

 

1. JPA Mini Book

2. JVM Troubleshooting Guide

3. JUnit Tutorial for Unit Testing

4. Java Annotations Tutorial

5. Java Interview Questions

6. Spring Interview Questions

7. Android UI Design

 

and many more ....

 

Receive Java & Developer job alerts in your Area

I have read and agree to the terms & conditions

 

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments