Name
declare
Synopsis
declare [options] [name[=value]] typeset [options] [name[=value]]
Declare variables and manage their attributes. In function bodies, variables are local, as if declared with the local command.
Options
- -a
Each name is an indexed array variable.
- -A
Each name is an associative array variable.
- -f
Each name is a function.
- -F
For functions, print just the function name and attributes, not the function definition (body). Implies -f.
- -i
Each variable is an integer; in an assignment, the value is evaluated as an arithmetic expression.
- -l
Assign all values as lowercase only; convert uppercase to lowercase.
- -p
With no names, print all variables and their values. With names, print the names, attributes, and values of the given variables. Used with -f, print all function names and attributes. 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 and RETURN traps from the shell. This attribute has no meaning for variables.
- -u
Assign all values as uppercase only; convert lowercase to uppercase.
- -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 valMake val an integer $val=4+7Evaluate value $echo $valShow result 11 $declare -r z=42Make ...
Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access