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

Create a free account to save this page, take notes and track your progress. Sign in

Syntax

declare [-a|-A|-i|-r|-x|-l|-u|-p] NAME[=VALUE]...

Parameters

ParameterDescription
-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

An associative array
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.

Case, integer and read-only attributes
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.

Was this page helpful?