Skip to content
Tutorials Center
My learning

Topics

Linux Command Reference ยท Shell Builtins

Bash declare command

Reference for the Bash declare builtin: integer, array, associative array, readonly, export, nameref, lowercase and uppercase attributes, and listing variables.

declare creates variables and gives them attributes: integer arithmetic, read-only, exported, array, associative array, lowercase, uppercase or a reference to another variable. Plain assignments make ordinary strings; declare is the tool when a variable needs more than that.

Inside a function, declare makes the variable local, just like local. typeset is an exact synonym kept for compatibility with ksh.

Terminal
declare [-aAfFgilnrtux] [-p] [NAME[=VALUE] ...]

Try it

Bash Integer, case and read-only attributes

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Bash Arrays, associative arrays and namerefs

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with read or cat), it gets the next line from this box.

Syntax

Terminal
declare [-aAfFgilnrtux] [-p] [name[=value] ...]
declare +attribute name          # remove an attribute
Name Description
-i Integer: assigned values are evaluated as arithmetic expressions.
-a Indexed array.
-A Associative array (keys are strings).
-r Read-only; cannot be changed or unset.
-x Exported to the environment (same as export).
-l, -u Convert the value to lowercase or uppercase on assignment.
-n Nameref: the variable is an alias for the variable named by its value.
-g Create or modify a global variable even inside a function.
-t Trace attribute for functions (inherit DEBUG and RETURN traps).
-f Act on functions; with no names, print all function definitions.
-F Print function names only (and file/line with extdebug).
-p Print the declaration of the named variables (or of all variables).
+attr Turn the attribute off (not possible for -r and for -a/-A conversions).

Returns: 0 on success, non-zero for an invalid option, a read-only variable, or a failed assignment.

Errors and edge cases: declare -a and declare -A cannot be converted into each other; use a new name. Associative arrays require declare -A before first use: map[key]=v without it treats key as an arithmetic expression on an indexed array.

Usage notes

  • Integer variables accept expressions without $(( )): declare -i total; total=2+3 yields 5. Strings that are not valid numbers silently become 0, and values exceed 64 bits wrap around.
  • Local by default in functions. declare name inside a function is local to it, so use -g for a global. Outside functions declare and plain assignment give the same scope.
  • Printing for reuse. declare -p var outputs a line that recreates the variable with its attributes, including arrays (declare -a colors=([0]="red" ...)). That is the right way to save a variable for eval or a file, much safer than echoing it.
  • Copying arrays. declare -a copy=("${orig[@]}") duplicates a list; assignments of the form copy=$orig only copy element 0.
  • Namerefs make it possible to return arrays from functions and pass names instead of values. Avoid name clashes between the nameref and its target (a function that has local -n arr fails if the caller's variable is also called arr).
  • Listing with no arguments prints every variable and function, like set does. declare -x lists exported variables, declare -r lists read-only ones.
  • declare -r vs readonly. They are the same operation. See local and readonly.
  • Case conversion applies to later assignments, not retroactively, but declare -u var converts the existing value immediately.