Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [98]

By Root 872 0
way to do this is to place them in a separate "library" file, and then read them with the dot command:

. my_funcs # read in functions

If the named file does not contain a slash, then the shell searches the directories in $PATH in order to find the file. The file does not need to be executable, just readable.

* * *

Tip


Everything in the read-in file is executed in the current shell. Thus, variable assignments, function definitions, and directory changes with cd all take effect. This is very different from simply running a separate shell script, which runs in a separate process and does not affect the current shell.

* * *

The set Command

The set command serves several conceptually distinct purposes.[7] It also uses an unusual option syntax, which the POSIX standard retains for historical compatibility. As a result, it is somewhat hard to learn.

* * *

set


Usage

set

set -- [ arguments ... ]

set [ - short-options ] [ -o long-option ] [ arguments ... ]

set [ + short-options ] [ +o long-option ] [ arguments ... ]

set -o

set +o

Purpose

To print the names and values of all current shell variables; to set or unset the value of shell options (which change the way that the shell behaves); and to change the values of the positional parameters.

Major options

See text.

Behavior

With no options or arguments, print the names and values of all shell variables in a form that can later be reread by the shell.

With -- and arguments, replace the positional parameters with the supplied arguments.

With short-form options that begin with a -, or long-form options that begin with -o, enable particular shell options. Additional nonoption arguments set the positional parameters. See text for details.

With short-form options that begin with a +, or long-form options that begin with +o, disable particular shell options. See text for details.

A single -o prints the current settings of the shell options "in an unspecified format." ksh93 and bash both print a sorted list, where each line is an option name and the word on or off:

$ set -o

From bash

allexport off

...

A single +o prints the current settings of the shell options in a way that they may be later reread by the shell to achieve the same set of option settings.

Caveats

Real shells have additional short and long options, above and beyond the ones described in Table 7-10. Details are given in Chapter 14. Don't use them if portability is a major concern.

Some versions of /bin/sh don't recognize set -o at all.

* * *

The simplest job of the set command is to print the names and values of all shell variables in sorted order. This is what it does when invoked with no options or arguments. The output is in a form that may later be reread by the shell, including quoting as appropriate. The idea is that it should be possible for a shell script to save its state, and then restore it later via the . (dot) command.

The next job for set is to change the positional parameters ($1, $2, etc.). By using a first argument of -- to end options to set itself, all following arguments replace the positional parameters, even if they start with a minus or plus sign.

Finally, set is used to enable or disable shell options, which are internal settings that change the way the shell behaves. Here's where the complexity comes in: historically, shell options were described with single letters, enabled with a minus sign and disabled with a plus sign. POSIX added long-name options, enabled or disabled with -o or +o. Each single-letter option has a corresponding long-name option. Table 7-10 lists the options, along with a brief description of what they do.

Table 7-10. POSIX shell options

Short option

-o form

Description

-a

allexport

Export all subsequently defined variables.

-b

notify

Print job-completion messages right away, instead of waiting for next prompt. Intended for interactive use.

-C

noclobber

Don't allow > redirection to existing files. The >| operator overrides the setting of this option.

Return Main Page Previous Page Next Page

®Online Book Reader