LinuxCommandLibrary

declare

Declare variable and set attributes

TLDR

Declare a string variable with the specified value

$ declare [variable]="[value]"
copy

Declare an integer variable with the specified value
$ declare -i [variable]="[value]"
copy

Declare an array variable with the specified value
$ declare -a [variable]=([item_a item_b item_c])
copy

Declare an associative array variable with the specified value
$ declare -A [variable]=([[key_a]=item_a [key_b]=item_b [key_c]=item_c])
copy

Declare a readonly string variable with the specified value
$ declare -r [variable]="[value]"
copy

Declare a global variable within a function with the specified value
$ declare -g [variable]="[value]"
copy

Print a function definition
$ declare -f [function_name]
copy

Print a variable definition
$ declare -p [variable_name]
copy

SYNOPSIS

declare [options] [name[=value] ...]
declare -f [name ...]
declare -p [name ...]

PARAMETERS

-a
    Declare an indexed array variable.

-A
    Declare an associative array variable.

-f
    Display function definitions and names.

-F
    Display function names and attributes only (without definitions).

-g
    Create or modify a global variable, even when inside a shell function (Bash 4.2+).

-i
    Declare an integer variable; values are evaluated as arithmetic expressions.

-l
    Convert variable to lowercase on assignment.

-p
    Display the attributes and values of specified variables, or all variables if none are specified.

-r
    Declare a readonly variable; its value cannot be changed or unset.

-t
    Declare a traced variable (used for debugging, typically by the shell itself).

-u
    Convert variable to uppercase on assignment.

-x
    Declare variable for export to the environment of child processes.

-n
    Create a nameref variable, which is a reference to another variable (Bash 4.3+).

DESCRIPTION

The declare command, a shell built-in (primarily in Bash, ksh, and zsh), is used to declare shell variables, set their attributes, and display the attributes and values of variables. It's often synonymous with typeset in Bash, originating from the Korn Shell. declare is powerful for controlling variable behavior, allowing you to define scope (e.g., global with -g in Bash 4.2+), assign initial values, or list existing variables with their attributes. It's crucial for scripting to ensure variables behave as expected, for instance, forcing arithmetic evaluation for integers or preventing modification of readonly variables. It can also be used to declare associative arrays or indexed arrays, providing flexible data structures.

CAVEATS

  • Built-in Command: declare is a shell built-in, not an external executable. This makes it faster and more efficient within shell scripts.
  • Shell Specificity: While prevalent in Bash, ksh, and zsh, its exact behavior and available options can vary slightly between different shell implementations and versions. For instance, options like -g and -n are specific to newer Bash versions.
  • Scope: When used inside a function without the -g option (or `local` in Bash), variables declared with `declare` are local to that function by default, shadowing global variables of the same name.

VARIABLE ATTRIBUTES EXPLAINED

declare allows assigning various attributes to variables, dictating their behavior. For example, an integer variable (-i) automatically performs arithmetic evaluation on assignment. A readonly variable (-r) cannot be reassigned or unset after its initial declaration. Exported variables (-x) are passed to the environment of child processes. Uppercase (-u) and lowercase (-l) attributes automatically convert assigned values. These attributes are crucial for robust script design and data integrity.

HISTORY

The declare command originated in the Korn Shell (ksh), where it was primarily known as typeset. Bash adopted `typeset` and later introduced `declare` as a synonym, largely making them interchangeable for many common uses. Over time, Bash has enhanced `declare` with unique functionalities (like associative arrays, namerefs, and global scope control with -g), making it a more powerful and versatile built-in for modern shell scripting compared to its `typeset` counterpart.

SEE ALSO

typeset(1), local(1), export(1), readonly(1), unset(1), env(1), printenv(1)

Copied to clipboard