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

SYNOPSIS

declare [options] [name[=value] ...]

PARAMETERS

-a
    Make each name an indexed array.

-A
    Make each name an associative array.

-f
    Display function definitions rather than variable values. When used with -g, functions are displayed using the location in source. With the -F option, list function names only (and optionally source file and line number when debugging).

-g
    Create or modify global variables instead of local.

-i
    Make each name an integer.

-l
    When the variable is assigned a value, convert all upper-case characters to lower-case.

-n
    Make each name a reference to the variable named by its value.

-p
    Display the attributes and value of each name. When no name arguments are supplied, display the attributes and value of all variables. When used with -f, display function definitions similarly.

-r
    Make each name read-only. These variables cannot be subsequently modified or unset.

-t
    Give each name the trace attribute. Traced functions inherit the DEBUG and RETURN traps from the calling context. The trace attribute has no special meaning for variables.

-u
    When the variable is assigned a value, convert all lower-case characters to upper-case.

-x
    Make each name exported to subsequent commands via the environment.

+option
    Use + instead of - turns off the attribute, with exception of -r: declare +r var makes var writable.

DESCRIPTION

The declare command in Linux is a powerful built-in command in Bash that allows you to declare shell variables and give them specific attributes. These attributes control how the variables behave. It is a counterpart of `typeset`. Using declare you can, for example, specify whether a variable is read-only, an integer, or an array. This command provides better control over your shell variables by allowing you to define their behavior. It is most useful in shell scripts, to help enforce good practices.

CAVEATS

The declare command is a Bash built-in, therefore its behavior and availability are tied to the Bash shell.
The '+r' syntax does not work as expected in older versions of Bash.

INTEGER ARITHMETIC

When declaring a variable as an integer using -i, Bash will automatically evaluate arithmetic expressions assigned to it. This simplifies performing calculations within scripts.

VARIABLE SCOPE

When used inside a function, declare creates local variables unless the -g option is specified to create a global variable within the function's scope. This can be used to have a global scope for a certain variable only when the function has been run.

DISPLAYING ALL VARIABLES

Using `declare -p` without any arguments allows to quickly view all declared variables and their attributes.

HISTORY

The declare command is a standard part of the Bash shell.
It is the bash's version of the older `typeset` command from ksh.
It evolved to provide more control over variable attributes within shell scripting.

SEE ALSO

typeset(1), export(1), readonly(1), local(1)

Copied to clipboard