Session and utilities
The declare command
Declares variables and arrays, and gives them attributes such as integer or read-only.
Most useful options
-aEach name is an indexed array, with numeric indexes.-AEach name is an associative array, with text keys.-iThe variable is an integer: when assigned, it is evaluated as arithmetic.-nTurns the variable into a name reference (nameref) to the variable whose name is its value.-rMarks the variable as read-only. It cannot be removed with +r.-xExports the variable to the environment of the programs you run afterwards.-pShows the attributes and value of each name, in a reusable format.-gInside a function, creates or changes the variable in the global scope.Copy-ready examples
$ declare COLOR=azul && echo ${COLOR}creates a variable and reads it.Example output
azul
$ declare COLOR=azul && declare -p COLORshows the declaration with its attributes.Example output
declare -- COLOR="azul"
$ declare -i n="2+3" && echo $nwith -i, the value is calculated when assigned.Example output
5
$ declare -a nombres=(ana luis) && echo ${nombres[1]}creates an indexed array and reads one element.Example output
luis
$ f() { declare -g G=7; }; f; echo $Gcreates a global variable from inside a function.Example output
7
You can try them in the browser terminal: it does not touch your computer.
Common errors
| Message | Cause | Fix |
|---|---|---|
| bash: declare: -q: invalid option | declare has no -q option. It is a typo or a mix-up with another command. | Check the options with help declare. The valid ones appear in brackets in the syntax. |
| bash: declare: x: readonly variable | The variable was already read-only and declare tried to change its value. | Use another name, or open a new terminal to start without the attribute. |
| bash: declare: `1x=2': not a valid identifier | The name starts with a number, and variable names cannot do that. | Start the name with a letter or an underscore, for example _1x=2. |
Typical mistakes
- declare +r does not remove the read-only attribute: you need to open a new terminal.
- Inside a function, declare creates a local variable, not a global one, unless you use -g.
- With -i, text that is not a number is evaluated as 0: declare -i n=abc; echo $n prints 0.
Differences from similar commands
- local: inside a function, declare without -g does the same as local. Outside one, declare creates globals and local is an error.
- export: marks variables for the environment. declare -x does the same and also accepts other attributes.
- readonly: marks read-only, like declare -r, but without the other attributes.
FAQ
What is the difference between declare and local?
local only works inside functions. declare also works outside and accepts more attributes; inside a function it behaves like local unless you use -g.
How do I see a variable's attributes?
With declare -p NAME, which prints something like declare -- COLOR="azul".
How do I remove an attribute?
Use + instead of -, for example declare +x NAME. There are exceptions: +r does not remove read-only, and +a or +A do not destroy an array.
Learn to actually use it
The basic Linux course takes you from zero to console fluency with 24 self-grading lessons.
Start the free course