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
-aEach name is an array.
-fEach name is a function.
-FFor functions, print just the function's name and attributes, not the function definition (body).
-iEach variable is an integer; in an assignment, the value is evaluated as an arithmetic expression.
-pWith no names, print all variables and their values. With names, print the names, attributes, and values of the given variables.
-rMark names as read-only. Subsequent assignments will fail, and read-only variables cannot be unset.
-tApply the trace attribute to each name. Traced functions inherit the
DEBUGtrap. This attribute has no meaning for variables.-xMark 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 z read-only $z=31Try to assign to it bash: z: readonly variable Assignment fails $echo $z42 $declare -p val zShow attributes and values declare -i val="11" declare -r z="42"
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