bash: Shell Scripts
1
1 3.8 Shell Scripts
1 =================
1
1 A shell script is a text file containing shell commands. When such a
1 file is used as the first non-option argument when invoking Bash, and
1 neither the '-c' nor '-s' option is supplied (⇒Invoking Bash),
1 Bash reads and executes commands from the file, then exits. This mode
1 of operation creates a non-interactive shell. The shell first searches
1 for the file in the current directory, and looks in the directories in
1 '$PATH' if not found there.
1
1 When Bash runs a shell script, it sets the special parameter '0' to
1 the name of the file, rather than the name of the shell, and the
1 positional parameters are set to the remaining arguments, if any are
1 given. If no additional arguments are supplied, the positional
1 parameters are unset.
1
1 A shell script may be made executable by using the 'chmod' command to
1 turn on the execute bit. When Bash finds such a file while searching
1 the '$PATH' for a command, it spawns a subshell to execute it. In other
1 words, executing
1 filename ARGUMENTS
1 is equivalent to executing
1 bash filename ARGUMENTS
1
1 if 'filename' is an executable shell script. This subshell
1 reinitializes itself, so that the effect is as if a new shell had been
1 invoked to interpret the script, with the exception that the locations
1 of commands remembered by the parent (see the description of 'hash' in
1 ⇒Bourne Shell Builtins) are retained by the child.
1
1 Most versions of Unix make this a part of the operating system's
1 command execution mechanism. If the first line of a script begins with
1 the two characters '#!', the remainder of the line specifies an
1 interpreter for the program. Thus, you can specify Bash, 'awk', Perl,
1 or some other interpreter and write the rest of the script file in that
1 language.
1
1 The arguments to the interpreter consist of a single optional
1 argument following the interpreter name on the first line of the script
1 file, followed by the name of the script file, followed by the rest of
1 the arguments. Bash will perform this action on operating systems that
1 do not handle it themselves. Note that some older versions of Unix
1 limit the interpreter name and argument to a maximum of 32 characters.
1
1 Bash scripts often begin with '#! /bin/bash' (assuming that Bash has
1 been installed in '/bin'), since this ensures that Bash will be used to
1 interpret the script, even if it is executed under another shell.
1