Declare shell variables with types and attributes.
low risk
declare sets variable attributes: -i for integers, -a for arrays, -A for associative arrays (hash maps), -r for read-only, -x for export. Enables type-checking and structured data in bash scripts.
| Flag | Meaning |
|---|---|
-i --integer | Treat variable as integer (arithmetic only) |
-a --array | Indexed array |
-A --assoc | Associative array (hash map) |
-r --readonly | Read-only variable |
-x --export | Export to environment |
-p --print | Print variable declaration |
declare -A config; config[host]='localhost'; config[port]=5432Create an associative array (hash map) · explain & risk-check →declare -i count=0; count+=1Integer variable with arithmetic · explain & risk-check →declare -r VERSION='1.2.3'Read-only constant · explain & risk-check →declare -p myvarPrint variable declaration for debugging · explain & risk-check →Use `declare -A` for config maps in bash scripts: store key-value pairs without external tools. Iterate with `for key in "${!map[@]}"; do echo "$key=${map[$key]}"; done`
Reference text is hand-written and may be incomplete — check man declare for your version. Test destructive commands on a non-production system first.