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.
declare [-aAfFgilnrtux] [-p] [NAME[=VALUE] ...]
Try it
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.
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
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+3yields 5. Strings that are not valid numbers silently become 0, and values exceed 64 bits wrap around. - Local by default in functions.
declare nameinside a function is local to it, so use-gfor a global. Outside functionsdeclareand plain assignment give the same scope. - Printing for reuse.
declare -p varoutputs 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 forevalor a file, much safer than echoing it. - Copying arrays.
declare -a copy=("${orig[@]}")duplicates a list; assignments of the formcopy=$origonly 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 arrfails if the caller's variable is also calledarr). - Listing with no arguments prints every variable and function, like
setdoes.declare -xlists exported variables,declare -rlists read-only ones. declare -rvsreadonly. They are the same operation. Seelocalandreadonly.- Case conversion applies to later assignments, not retroactively, but
declare -u varconverts the existing value immediately.
Related
localandreadonly,export,unset,mapfile,read- Lessons: Variables, Arrays, Arithmetic