Executables in Linux

If you are familiar with Windows, then you know that there is a type of file that we call Executable. It uses .exe as the file format, and you can run such a file by double-clicking on it. You can also run such a file by typing its name inside the command line and hitting Enter.

When you enter the world of Linux, you may realize that there is no fixed executable format. Long-time Linux users know that in Linux, you can run any kind of file as an executable so long as its executable flag has been set. When you run the ls command in a folder, the files which can be executed appear green, and you can type dot, slash, and then the name of that file and hit Enter to run that file, as seen in this picture:

When the green file ‘greet ‘ in the above picture was run, it executed some Linux shell commands. In fact, the file greet is a text file containing one line of code:

echo Hello, World!

By default, any text file with lines of Linux shell commands inside it can be turned into an executable file by setting its Executable Flag, which is done with the command:

chmod +x <filename>

But sometimes, you want to run a text file which has commands in Python, Perl, Ruby, Javascript, etc. languages instead of Linux Shellscript. That is where the Shebang comes into the picture.

Using Shebang in Linux

Imagine you’re working on a Linux computer and have a recipe with instructions written in different languages. But your computer only understands English! That’s where the shebang comes in. It’s like a tiny label at the beginning of the recipe that tells your computer which “translator” to use for each language. This magic symbol, #!, followed by the interpreter’s path, is the key to understanding and running all sorts of scripts on your system.

Similar Reads

Evolution and Historical Significance of Shebang

The shebang has a cool history. It started way back in an old operating system called Multics, but it really took off with Unix. There, it became the go-to way for computers to figure out which program to use for different scripts. Now, it’s not just for shell scripts anymore! The shebang works for all sorts of scripting languages, making it a cornerstone of how Linux and its friends run these handy tools...

Practical Application of the Shebang

In some Linux programs that are written in scripting languages like Python, Perl, and Shell-script, you will see a line right at the top that starts with #!/ like this one:...

Executables in Linux

If you are familiar with Windows, then you know that there is a type of file that we call Executable. It uses .exe as the file format, and you can run such a file by double-clicking on it. You can also run such a file by typing its name inside the command line and hitting Enter....

A Python Example

Suppose we wish to make an executable in Python. We can write a Python script which will input a number and return its square.We want to run this executable. If we have Python installed, we can run the file simply by using the following command:...

Using a Shebang in Linux

You can use a shebang to tell Linux which interpreter is needed to run the script. For instance, in the case of a Python file, you will need the python executable. Usually, this is located in /usr/bin. You will need the complete path to the executable....

Making Shebangs Portable – Using env

Of course, if you write a script with a Shebang on your machine, it may not work on other machines. For instance, another person may not have Python installed. Also, another person may not have Python installed in the same location. So how do we make the Shebang portable?...

Executable Path and Shell Commands

When you use an executable script in Linux with a shebang, you can make it become a shell command! To do this, you need to place it in a PATH location. A PATH location is a special folder in your Linux filesystem, and all executables inside a PATH location can be used like commands in Linux....

Adding Your File to a PATH Location

First, look up the existing PATH locations using this command:...

Adding a Folder to the PATH List

In general, it is a good idea to make yourself a new directory, add all your executable script files there, and then add that directory to the PATH locations. This is especially useful if you do not already have a folder named bin inside ~/.local directory....

Some Other Uses

Shebangs allow you to choose the interpreter which runs a script, so that the user does not have to select the interpreter at run time. This can be used to turn a script into a command, but it also has some other uses.We are going to go over some of them....

Possible Malicious Use

Its always a good idea to be warned of all the ways in which something can be used for evil. Shebang can be used for evil. A shebang only specifies the path to an executable, and the OS assumes that the executable is an interpreter to run the script on. But we can run any executable using a shebang....

Conclusion

In this article we discussed Linux shebang which is a special code, marked by #!, at the start of a script that tells a computer which language the script is written in. Originating from Multics and gaining popularity with Unix, the shebang is crucial for running scripts in various languages on Linux systems. This article explored its history, practical applications, and versatility, demonstrating its use beyond shell scripts for languages like Python. It explained how shebangs enable the conversion of text files into executable ones, using commands like ‘chmod +x.’ The article also covered making shebangs portable with specific executable names and the ‘env’ command. Lastly, it highlighted additional uses, such as ensuring compatibility across different shells, specifying interpreter versions, and preventing direct execution, while urging caution against potential malicious uses by reviewing shebangs before running scripts....