Shebang: Difference between revisions

From NovaOrdis Knowledge Base
Jump to navigation Jump to search
 
(7 intermediate revisions by the same user not shown)
Line 8: Line 8:


=Overview=
=Overview=
The <code>#!</code> sequence of characters at the beginning of a file indicates that the file is a script: a set of commands to be fed into an interpreter. The #! is actually a two-byte magic number, a special marker that designates a file type, or in this case an executable script. The path that follows <code>#!</code> is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or a utility. The interpreter executes the commands in the script, starting with the line following the <code>#!</code> line:
<syntaxhighlight lang='text'>
#! <interpreter> [optional-args]
</syntaxhighlight>
=The Path Must be Absolute=
The interpreter should always be specified by absolute path to ensure that the script can be executed from any directory. In case of relative path, it's generally a typo and shell static checkers usually warn against this syntax. Alternatively use [[env|/usr/bin/env]].
=Bash=
<syntaxhighlight lang='bash'>
#!/usr/bin/env bash
echo "."
</syntaxhighlight>
=Python=
<syntaxhighlight lang='bash'>
#!/usr/bin/env python
print('.')
</syntaxhighlight>

Latest revision as of 00:35, 2 May 2023

External

Internal

Overview

The #! sequence of characters at the beginning of a file indicates that the file is a script: a set of commands to be fed into an interpreter. The #! is actually a two-byte magic number, a special marker that designates a file type, or in this case an executable script. The path that follows #! is the path to the program that interprets the commands in the script, whether it be a shell, a programming language, or a utility. The interpreter executes the commands in the script, starting with the line following the #! line:

#! <interpreter> [optional-args]

The Path Must be Absolute

The interpreter should always be specified by absolute path to ensure that the script can be executed from any directory. In case of relative path, it's generally a typo and shell static checkers usually warn against this syntax. Alternatively use /usr/bin/env.

Bash

#!/usr/bin/env bash
echo "."

Python

#!/usr/bin/env python
print('.')