Ubuntu Unleashed 2019 Edition: Covering 18.04, 18.10, 19.04

(singke) #1

The majority of shell scripts use a shebang line (#!) at the beginning to
control the type of shell used to run the script; this bang line calls for an sh-
incantation of bash:


#!/bin/sh

A shebang line (it is short for “sharp” and “bang,” two names for # and !)
tells the Linux kernel that a specific command (a shell, or in the case of other
scripts, perhaps awk or Perl) is to be used to interpret the contents of the file.
Using a shebang line is common practice for all shell scripting. For example,
if you write a shell script using bash but want the script to execute as if run
by the Bourne shell, sh, the first line of your script contains #!/bin/sh,
which is a link to the bash shell. Running bash as sh causes bash to act as
a Bourne shell. This is the reason for the symbolic link sh, which points to
bash.


THE SHEBANG LINE
The shebang line is a magic number, as defined in
/usr/share/misc/magic—a text database of magic numbers for the
Linux file command. Magic numbers are used by many different Linux
commands to quickly identify a type of file, and the database format is
documented in the section five manual page named magic (read by using
man 5 magic). For example, magic numbers can be used by the Linux
file command to display the identity of a script (no matter what filename
is used) as a shell script using a specific shell or other interpreter, such as
awk or Perl.

You might also find different or new environmental variables available to
your scripts by using different shells. For example, if you launch csh from
the bash command line, you find several new variables or variables with
slightly different definitions, such as the following:


Click here to view code image
matthew@seymour:~$ env
...
VENDOR=intel
MACHTYPE=i386
HOSTTYPE=i386-linux
HOST=thinkpad.home.org


On the other hand, bash might provide these variables or variables of the
same name with a slightly different definition, such as the following:


Click here to view code image

Free download pdf