Online Book Reader

Home Category

Classic Shell Scripting - Arnold Robbins [67]

By Root 981 0
inline arithmetic called arithmetic expansion. The shell evaluates arithmetic expressions inside $((...)), and places the result back into the text of the command.

Variable Assignment and the Environment

Shell variable assignment and usage were covered in Section 2.5.2. This section fills in the rest of the details.

Two similar commands provide variable management. The readonly command makes variables read-only; assignments to them become forbidden. This is a good way to create symbolic constants in a shell program:

hours_per_day=24 seconds_per_hour=3600 days_per_week=7 Assign values

readonly hours_per_day seconds_per_hour days_per_week Make read-only

* * *

export, readonly


Usage

export name[= word] ...

export -p

readonly name[= word] ...

readonly -p

Purpose

export modifies or prints the environment. readonly makes variables unmodifiable.

Major options

-p

Print the name of the command and the names and values of all exported (read-only) variables in such a way as to allow the shell to reread the output to re-create the environment (read-only settings).

Behavior

With the -p option, both commands print their name and all variables and values that are exported or read-only, respectively. Otherwise, they apply the appropriate attribute to the named variables.

Caveats

The versions of /bin/sh on many commercial Unix systems are (sadly) still not POSIX-compliant. Thus the variable-assignment form of export and readonly don't work. For strictest portability, use:

FOO=somevalue

export FOO

BAR=anothervalue

readonly BAR

* * *

Much more commonly used is the export command, which puts variables into the environment. The environment is simply a list of name-value pairs that is available to every running program. New processes inherit the environment from their parent, and are able to modify it before creating new child processes of their own. The export command adds new variables to the environment:

PATH=$PATH:/usr/local/bin Update PATH

export PATH Export it

The original Bourne shell required you to use a two-step process; i.e., the assignment and the export or readonly are done separately (as we've just shown). The POSIX standard allows you to do the assignment and command together:

readonly hours_per_day=24 seconds_per_hour=3600 days_per_week=7

export PATH=$PATH:/usr/local/bin

The export command may also be used to print the current environment:

$ export -p

Print current environment

export CDPATH=":/home/tolstoy"

export DISPLAY=":0.0"

export ENV="/home/tolstoy/.kshrc"

export EXINIT="set ai sm"

export FCEDIT="vi"

...

Variables may be added to a program's environment without permanently affecting the environment of the shell or subsequent commands. This is done by prefixing the assignment to the command name and arguments:

PATH=/bin:/usr/bin awk '...' file1 file2

This changes the value of PATH only for execution of the single awk command. Any subsequent commands, however, see the current value of PATH in their environment.

The export command only adds variables to the environment. The env command may be used to remove variables from a program's environment, or to temporarily change environment variable values:

env -i PATH=$PATH HOME=$HOME LC_ALL=C awk '...' file1 file2

The -i option initializes the environment; i.e., throws away any inherited values, passing in to the program only those variables named on the command line.

* * *

env


Usage

env [ -i ] [ var = value ... ] [ command_name [ arguments ... ] ]

Purpose

To provide fine-grained control over the environment inherited by command_name when it's run by env.

Major options

-i

Ignore the inherited environment, using only the variables and values given on the command line.

Behavior

With no command_name, print the names and values of all variables in the environment. Otherwise, use the variable assignments on the command line to modify the inherited environment, before invoking command_name. With the -i option, env ignores the inherited environment completely and

Return Main Page Previous Page Next Page

®Online Book Reader