Bash 5 reference · Shell Builtins and Syntax
declare (Bash builtin)
Declare variables with attributes: arrays, associative arrays, integers, read-only values and exported variables.
Bash 5
Syntax
declare [-a|-A|-i|-r|-x|-l|-u|-p] NAME[=VALUE]...
Parameters
| Parameter | Description |
|---|---|
-A |
Associative array (map). |
-i |
Integer: assignments are evaluated arithmetically. |
-r |
Read-only. |
-x |
Export to child processes. |
-l / -u |
Lowercase / uppercase on assignment. |
-p |
Print declarations. |
Examples
declare -A port=([web]=80 [api]=8080)
echo "api on ${port[api]}, ${#port[@]} services"
Output
api on 8080, 2 services
Run this on your computer — see “Get Started” for setup.
declare -u code=abc; declare -i n=2+3
echo "$code $n"
declare -r FIXED=1
(FIXED=2) 2>/dev/null || echo 'FIXED is read-only'
Output
ABC 5 FIXED is read-only
Run this on your computer — see “Get Started” for setup.
Related
arrays· Indexed and associative arrays: create, append, read elements, count, slice and loop over them safely.local· Declare variables that exist only inside the current function, so they don't overwrite globals with the same name.export· Mark variables so child processes inherit them; one-off variables can also be set for a single command.- Bash Arrays and Associative Arrays · tutorial