# Bash - Declare (Variable declaration and attribute)

Declare variables and/or give them attributes.

## 3 - Syntax

declare [-afFirtx] [-p] [name[=value] ...]
typeset [-afFirtx] [-p] [name[=value] ...]

where:

• names: If no names are given then display the values of variables.
• p will display the attributes and values of each name. When -p is used, additional options are ignored.
• F option inhibits the display of function definitions; only the function name and attributes are printed. If the extdebug shell 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:

• -a Each name is an array variable.
• -f Use function names only.
• -i The variable is treated as an integer; arithmetic evaluation is performed when the variable is assigned a value.
• -r Make names readonly. See also the readonly builtin command. These names cannot then be assigned values by subsequent assignment statements or unset.
• -t Give each name the trace attribute. Traced functions inherit the DEBUG and RETURN traps from the calling shell. The trace attribute has no special meaning for variables.
• -x Mark names for export to subsequent commands via the environment.

Using + 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 -f foo=bar,
• 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,
• or an attempt is made to display a non-existent function with -f.