Bootstrap FreeKB - Bash (Scripting) - Understanding script interpreter
Bash (Scripting) - Understanding script interpreter

Updated:   |  Bash (Scripting) articles

When viewing or creating a script, regardless if the script is a bash shell script or some other language such as Python or NodeJS or Perl, line 1 of the script is almost always the absolute path to the CLI that should be used. This is often refered to as the shebang or script interpreter. 

In this example, /bin/bash is the CLI that should be used.

#!/bin/bash

 

The interpreter tells Linux what language the script was written in so that the appropriate interpreter can be used when running the script. Imagine the script interpreter like a translator. You've a bunch of people speaking different languages (English, Mandarin, Spanish, Russian). Fortunately, the interpreter is really smart and understands each language.

 

Technically speaking, the #! characters are the shebang.

Following are some popular script interpreters.

  • #!/bin/sh = Legacy shell
  • #!/bin/bash = Bourne again shell
  • #!/bin/csh = C shell
  • #!/bin/ksh = Korn shell
  • #!/usr/bin/perl = Perl (.pl)
  • #!/usr/bin/python = Python (.py)

 

Each script interpreter has its own unique syntax.

It is important to recongize that a script does not require the line 1 shebang, but it is definitely best practise to start each script with a line 1 shebang. For example, let's say you have the following in a file named mystery.script.

~]# cat mystery.script
print("Hello World")

 

Since mystery.script does not include a line 1 shebang, it's not obvious what language this script was written in. Of course, if you are familiar with the syntax of different popular languages, you can probably determine the language, but wouldn't it just be more obvious to include a line 1 shebang that lists the CLI so the language is known?




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter 67d768 in the box below so that we can be sure you are a human.