Name

declare

Synopsis

declare [options] [name[=value]]

Declare variables and manage their attributes. In function bodies, variables are local, as if declared with the local command. See also typeset.

Options

-a

Each name is an array.

-f

Each name is a function.

-F

For functions, print just the function's name and attributes, not the function definition (body).

-i

Each variable is an integer; in an assignment, the value is evaluated 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.

-r

Mark names as read-only. Subsequent assignments will fail, and read-only variables cannot be unset.

-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 fails
$ echo $z
42

$ declare -p val z				Show attributes and values
declare -i val="11"
declare -r z="42"

Get bash Quick Reference now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.