
660
|
Chapter 6: The Bash Shell and Korn Shell
This is the Title of the Book, eMatter Edition
Copyright © 2006 O’Reilly & Associates, Inc. All rights reserved.
declare
declare [options] [name[=value]]
Bash only. Declare variables and manage their attributes. In func-
tion bodies, variables are local, as if declared with the local
command.
Options
-a Each name is an array.
-f Each name is a function.
-F For functions, print just the functions’ name and attributes,
not the function definition (body).
-i Each variable is an integer; in an assignment, the value is eval-
uated as an arithmetic expression.
-p With no names, print all variables and their values. With
names, print the names, attributes, and values of the given
variables. This option causes all other options to be ignored.
-r Mark names as read-only. Subsequent assignments will fail.
-t Apply the trace attribute to each name. Traced functions
inherit the DEBUG trap. This attribute has no meaning for
variables.
-x Mark names for export into the environment of child
processes.
With a + instead of a -, the given attribute is disabled. With no
variable names, all variables having the given attribute(s) are
printed in a form that can be reread as input to the shell.
Examples
$ declare -i val Make val an integer
$ val=4+7 Evaluate value
$ echo $val Show result
11
$ declare -r z=42 Make z read-only
$ z=31 Try to assign to it
bash: z: readonly variable Assignment ...