The UNIX system contains several shells; these are interactive command
interpreters. This manual page describes how the Bourne, C, and
Korn shells (see
sh(C),
csh(C),
and
ksh(C))
execute commands that you have entered.
When you enter a command, the shell tries to match it against
several possible entities in order to run it:
user-defined aliases (in csh and ksh)
built-in commands; for example cd, and echo
user-defined functions in the shell (in sh and
ksh)
executable files in each of the directories defined in the
shell's search path
If you specify an explicit pathname for a command, the shell will
attempt to execute that file instead of following steps 1 to 4.
There are two types of executable file that may be run:
lists of shell commands (see
``Shell scripts'')
that can be interpreted by the shell
You can run an executable file if it is a regular file or a
symbolic link to a regular file (it cannot be a directory,
a device special file, or a named pipe), and one of the
following conditions is met:
it has execute permission by everyone (shell scripts must also
have read permission by everyone)
it has execute permission by group (shell scripts must also
have read permission by group) and it is owned by your
effective group ID or one of the groups in your
current group list
it has execute permission by owner (shell scripts must also have
read permission by owner) and it is owned by your
effective user ID
The Bourne and C shells use the
fork(S)
system call to generate a new child process that is a copy of
the current shell. The child process exists so that your command can take
it over; the child invokes the
exec
system call to run the command in place of the child shell
without creating a new process.
(exec is also a shell built-in command; it calls
exec
without doing a fork; the original shell process
is replaced by the command given as an argument to exec.)
If exec cannot execute the file, the child shell assumes
the file is a shell script and runs it itself.
The Korn shell tries to avoid forking if it can. It must fork if
the file being executed is not a Korn shell script or if it forms
the second or subsequent element of a command pipeline; in these
two cases execution proceeds as for the Bourne and C
shells. Whenever possible, the Korn shell interprets and runs
the commands in the file without calling
fork.
The shell protects the main execution environment from the
effects of commands in the script; this emulates the
behavior of a forked process without the consequent overhead.
Execution of a file by exec(S)
exec
looks at the start of the file and tries to determine
its type; exec can run COFF and OMF
format binary executables, and #! scripts.
Some systems also allow DOS batch and binary executable files
to be run.
Binary executables
Binary executables files have been compiled from program source
code and linked into a loadable module. The executable contains a
code (the file's magic number)
in its header record that encodes the type of executable file.
(See
magic(F)
for more information.) Depending on this type,
it may be loaded and run directly on the processor,
or read by an emulator that is capable of converting
the machine instructions into a form that the processor can
understand. The following executable module types can be run:
Module type
Emulator or direct
i386 ELF binary
direct
i386 COFF binary
direct
i386 OMF (x.out) binary
direct
DOS executable or batch file
emulator (if available)
If a DOS emulator is available on your system,
exec uses it to run DOS executable and batch files.
See
dos(MERGE)
for more information.
Shell scripts
A shell script is a program written in the command language of
one of the shell programs. The shells deal with scripts in
different ways:
The Bourne shell forks a child shell to read and run a shell script.
The C shell forks a child shell to read and run a shell
script. The child shell executes /bin/sh to run
the script if the first character of the script is ``:'';
otherwise, it reads and runs the script without executing
a new shell.
The Korn shell reads and runs the script as a sub-shell (an
emulation of a child shell); it does not fork a child shell to
interpret a shell script unless the script forms the second or
subsequent element of a command pipeline.
#! scripts
If the first line of an executable file is of the form:
#!interpreter [ argument ]
exec
appends all command line arguments (including $0)
to the command interpreter and a single optional
argument, and loads the resulting
command to replace the child shell (see
``Examples'').
interpreter must always be fully specified including
its path; the form of
exec
that is used does not search the directories defined by the PATH
environment variable.
If the second and subsequent lines of the script are to be
interpreted by interpreter, the
script must be readable by the invoking user, or
interpreter must change the effective user or group
ID to one that can read it.
#! scripts are also sometimes referred to as
``hash-bang'', ``hash-pling'', or even
``hash-shriek'' scripts.
Examples
A one-line #! script (named bing) that
displays all command line arguments (including $0)
prefixed by the string ``args:'':
#!/bin/echo args:
Entering bing one two three four displays:
args: pathname/bing one two three four
pathname is displayed as an absolute or relative
path to the bing script.
A #! script that must always be executed
by /bin/sh:
#!/bin/sh
...
A common use of #! is to introduce an executable
awk(C)
script:
#!/usr/bin/awk -f
#
BEGIN { FS = ":" }
...
Similarly, #! can be used to introduce a
sed(C)
script:
#!/bin/sed -f
#
s/foo/bar/
...
Limitations
The interpretation of #! is a feature of
exec
and cannot be disabled.
Only one argument can be passed to the interpreter
named after the #! entry. Some interpreters
require the use of an option to tell them that the next argument
is the script filename. For example, you must specify the
-f option with both awk and sed.
The interpreter must understand the use of ``#''
to begin comment lines.
Earlier versions of the operating system either did not include
the #! feature, or allowed it to be disabled.
A DOS emulator must be available on your system in
order to run DOS executables and batch files.
Files
/etc/magic
a list of magic number definitions used by
file(C)