Explaining Sha-Bang for Bash Users
The aim of this page📝 is to conceptualize sha-bang for bash users.
In a large parts there are notes of / thoughts about a great Starting Off With a Sha-Bang
SHA-BANG IS SURPRISINGLY NOT REQUIRED
- a shell script can just as well a set of commands
- think of a
.bat
file in Windows universe - saves not just re-typing
- becomes soft_ware
- it can easily be shared modified or customized for a particular application
#!
can be omitted if the script consists only of a set of generic system commands, using no internal shell directives- If Bash is your default shell, then the #! isn’t necessary at the beginning of a script.
- However, if launching a script from a different shell, such as tcsh, then you will need the #!.
- e.g. make sure you have the
#!/bin/bash
shebang line for your script if you use double brackets for conditional expressions () [[
originates from bash and is generally treated as bash-specific
BUT IT IS USUALLY USED IN THE FIRST LINE
- this is the canonical form
- it informs the system about the particular file type of what’s following
#!/usr/bin/bash
THE #! IS MAGICAL
#!
is encoded as a two-byte magic number- a special marker that designates a file type
- in this case it is an executable shell script
- see
man magic
for more on this and also
A magic number is a sequence of bytes that is used in all files of a certain format, usually at a given position (often at the beginning). Since all files in that particular format have that particular byte sequence in that particular position, and most files in other formats don’t have it, the magic number is a way to recognize what format a file is in … The file command recognizes files based on their magic numbers. You can run file -s /dev/sda1 to see what it thinks is on the partition /dev/sda1. Its decisions are based on a database typically found in /etc/magic or /usr/share/misc/magic. Specific commands may use different magic numbers to recognize the file formats they can cope with.
— https://superuser.com/a/239114/1083809
file foo_0.1.0.sh
foo_0.1.0.sh: Bourne-Again shell script, ASCII text executable
- removed
#!/usr/bin/bash
from the first line of a file, I'm getting
file foo_0.1.0.sh
foo_0.1.0.sh: ASCII text executable
SHA-BANG IS NOT SHEBANG
sha
for sharpbang
for exclamation mark- not shebang < Search Online Etymology Dictionary !
- The phrase the whole shebang is recorded from 1869 > false friendship!
While I’m in London, I want to see Big Ben, the palace, the whole shebang.
ALSO, AN INCORRECT PATH TO THE INTERPRETER STOPS THE EXECUTION
- the path given at the “sha-bang” must be a proper one
- the error message is returned and the script won’t run, for example appending
2
to the sha-bang
bash: ./foo_0.1.0.sh: /usr/bin/bash2: bad interpreter: No such file or directory
FINALLY, IT IS POSSIBLE TO USE ENVIRONMENTAL VARIABLES INSTEAD OF PATHS PROPER
- use
env
when the path to the shell or interpreter is unknown
#! /usr/bin/env perl
print "This Perl script will run,\n";
print "even when I don't know where to find Perl.\n";
# Good for portable cross-platform scripts,
# where the Perl binaries may not be in the expected place.
# Thanks, S.C.
Or even …
#!/bin/env bash
# Queries the $PATH environmental variable for the location of bash.
# Therefore ...
# This script will run where Bash is not in its usual place, in /bin.
— both from https://tldp.org/LDP/abs/html/sha-bang.html