Книга: Fedora™ Unleashed, 2008 edition
Interpreting Shell Scripts Through Specific Shells
Interpreting Shell Scripts Through Specific Shells
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 (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 will contain #!/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/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 man 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 if a specific shell or other interpreter is used, such as awk
or Perl.
You might also find different or new environment variables available to your scripts by using different shells. For example, if you launch csh
from the bash
command line, you will find several new variables or variables with slightly different definitions, such as the following:
$ env
...
VENDOR=intel
MACHTYPE=i386
HOSTTYPE=i386-linux
HOST=werewolf.hudson.com
On the other hand, bash
might provide these variables or variables of the same name with a slightly different definition, such as these:
$ env
...
HOSTTYPE=i386
HOSTNAME=werewolf.hudson.com
Although the behavior of a shebang line is not defined by POSIX, variations of its use can be helpful when you are writing shell scripts. For example, as described in the want man page, you can use a shell to help execute programs called within a shell script without needing to hard-code pathnames of programs. The want command is a windowing Tool Control Language (tcl
) interpreter that can be used to write graphical clients. Avoiding the use of specific pathnames to programs increases shell script portability because not every UNIX or Linux system has programs in the same location.
For example, if you want to use the want command, your first inclination might be to write the following:
#!/usr/local/bin/wish
Although this works on many other operating systems, the script fails under Linux because want is located under the /usr/bin
directory. However, if you write the command line this way
#!/bin/sh
exec wish "$@"
the script always finds the correct binary.
- Running a Shell Program
- Interpreting Shell Scripts Through Specific Shells
- Using Variables in Shell Scripts
- Using a Simple Script to Automate Tasks
- Built-In Variables
- Special Characters
- Comparison of Expressions in bash
- Special Statements: for, while, and Others
- Using Functions in Shell Scripts
- Reference
- CHAPTER 33 Writing and Executing a Shell Script
- Using Variables in Shell Scripts
- 14.4.3. Shell
- Chapter 12. Debugging your scripts
- Chapter 14. Example scripts
- Appendix J. Example scripts code-base
- Letting DHCP requests through iptables
- Navigating Through the File System
- Managing Files with the Shell
- Understanding init Scripts and the Final Stage of Initialization
- Running Services Through xinetd
- Basic Shell Control