Using Shebang #! in Linux Scripts


Introduction

On Linux, a shebang (#!) is a special line at the beginning of a script that tells the operating system which interpreter to use when executing the script. This line, also known as a hashbang, shabang or "sharp-exclamation", is the first line of a dash and starts with "#!" followed by the path to the interpreter. The Shebang line is important because it allows you to run scripts written in any language, not just shell scripts, on a Linux system.

Understanding Shebang

What is a Shebang?

The Shebang “#!” The symbol indicates which interpreter, or which version of an interpreter, to use when running a script. It is also known as a "sharp-exclamation", "sha-bang", "hash-bang", or "pound-bang". The name is believed to have originated as a partial contraction of "SHarp bang" or "haSH bang".

A Shebang is always the first line of a script. Since it begins with the ‘#’ symbol, the line containing Shebang is not processed by the shell. When a Linux system executes a text file, it treats Shebang as a shell directive. It locates the correct interpreter and runs it, passing the filename to the interpreter as input. For example, running a file named “~/scripts/shebang” that starts with Shebang “#!/bin/sh” is functionally equivalent to running the “/bin/sh” “~/scripts/shebang” command. The text file must be executable for proper processing to occur.

The importance of Shebang

The Shebang directive has the following advantages −

  • Permits users to treat scripts and files executed as commands.

  • Hides certain implementation details from users, such as the name of the interpreter.

  • Does not require the user to know the absolute path to the interpreter or how to use the env command.

  • Allows a particular version of an interpreter to be used, for example, python2 versus python3.

  • Allows the interpreter to be changed while maintaining the same user behavior and command.

  • Can automatically pass mandatory options through to the interpreter.

A potential disadvantage can occur if the interpreter path is hard-coded. If the position of the interpreter changes, the Shebang directive must be updated at the same time. Otherwise the script may stop working.

Using Shebang in scripts

The Shebang format

The Shebang directive follows this format −

#!interpreter [options]

Here is a real example of a Shebang instruction. This Shebang requires using the Bourne sh shell to run the script. This example uses an absolute path to define the interpreter.

#!/bin/sh

The env utility can help find the path to the interpreter. In this case, Shebang tells the system to use “/usr/bin/env” to find out the path to the python2 interpreter. This technique is more robust because it continues to work if the path changes.

#!/usr/bin/env python2

Make the script executable

After adding the shebang line to your script, you need to make the script executable. To do this, you can use the chmod command with the ‘+x’ option. For instance −

$ chmod +x script.py

This command makes the “script.py” file executable.

Running the script

After making the script executable, you can run it by simply typing its name on the command line and pressing Enter. For instance −

$ ./script.py

This command runs the ‘script.py’ file.

Shebang best practices

Rules to follow

To effectively implement a Shebang, keep the following rules in mind.

  • The directive must always start with “#!” combination of characters

  • For this to work properly, a Shebang must appear on the first line of the file. If it is found elsewhere, it is treated as a comment.

  • Specify the full absolute path to the interpreter or use env to find the correct path. Insert interpreter options after the interpreter name. The implementation details of compiler options vary between different systems. However, all major operating systems support at least one option.

  • One or more spaces between #! The combination of characters and interpreter name is allowed, but not required. For example, the #!interpreter and #! directives interpreters are valid and functionally equivalent.

  • Linux allows a second script to act as an interpreter for the first script, but this isn't the case for all operating systems.

Shebang Special Directive

The “#!/bin/false” directive is a special Shebang. It exits immediately and returns an error status. Prevents certain system files from running outside their proper context.

Conclusion

The Shebang line is an important tool that allows you to run scripts written in any language on a Linux system. Tells the operating system which interpreter to use when running the script, and is the first line of the script. The Shebang line has several advantages, such as allowing the user to treat scripts and files as commands, hiding implementation details, and allowing a particular version of an interpreter to be used. Once you've added the shebang line to your script and made it executable, you can run it by simply typing its name on the command line. With the help of the Shebang line, you can easily run scripts written in any language on a Linux system with the help of the appropriate interpreter. It is important to note that if the shell path is hard-coded and if the shell location changes, the shebang directive must be updated in the shell.

Updated on: 13-Feb-2023

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements