Bash - Declare (Variable declaration and attribute)
1 - About
Declare variables and/or give them attributes.
2 - Articles Related
3 - Syntax
declare [-afFirtx] [-p] [name[=value] ...] typeset [-afFirtx] [-p] [name[=value] ...]
- names: If no names are given then display the values of variables.
pwill display the attributes and values of each name. When -p is used, additional options are ignored.
Foption inhibits the display of function definitions; only the function name and attributes are printed. If the
extdebugshell option is enabled using shopt, the source file name and line number where the function is defined are displayed as well. The -F option implies -f.
The following options can be used to restrict output to variables with the specified attribute or to give variables attributes:
-aEach name is an array variable.
-fUse function names only.
-rMake names readonly. See also the readonly builtin command. These names cannot then be assigned values by subsequent assignment statements or unset.
-tGive each name the trace attribute. Traced functions inherit the
RETURNtraps from the calling shell. The trace attribute has no special meaning for variables.
-xMark names for export to subsequent commands via the environment.
+ instead of
- turns off the attribute instead, with the exception that
+a may not be used to destroy an array variable.
4 - Scope
When used in a function, makes each name local, as with the local command.
If a variable name is followed by
=value, the value of the variable is set to value.
5 - Return value
The return value is 0 unless:
- an invalid option is encountered,
- an attempt is made to define a function using
- an attempt is made to assign a value to a readonly variable,
- an attempt is made to assign a value to an array variable without using the compound assignment syntax
- one of the names is not a valid shell variable name,
- an attempt is made to turn off read-only status for a readonly variable,
- an attempt is made to turn off array status for an array variable,
- or an attempt is made to display a non-existent function with