- Unix Commands Reference
- Unix Commands - Home
declare Command in Linux
In Linux bash scripts, the declare command declares variables and their attributes. It is a powerful tool to efficiently manage variables in the bash scripts.
It is important to note that Bash is not a strongly typed language. While it is not necessary to declare variables explicitly, the declare command is used in advanced projects where precise variable management is required.
Table of Contents
Here is a comprehensive guide to the options available with the declare command −
- Syntax of declare Command
- declare Command Options
- Attributes for declare Command
- Examples of declare Command in Linux
Syntax of declare Command
The syntax for using the bash declare command is as follows −
declare [options] [name]=[value]
In the above syntax, [options] will be replaced with options or attributes, while [name] is the variable name, and [value] signifies the variable value.
declare Command Options
Options that are used with the bash declare command are listed below −
Options | Description |
---|---|
-f | To declare a function |
-F | To print the function names and attributes |
-p | To print the variable value and attributes |
-g | To declare the global variable |
-I | Inherits value or attributes of previously declared variable with the same name |
Attributes for declare Command
Attributes that are assigned to a variable using the declare command are given below −
Options | Description |
---|---|
-a | The variable is declared as an indexed array |
-A | The variable is declared as an associative array |
-i | The variable is declared as an integer |
-l | The value of the variable will be converted to lowercase |
-u | The value of the variable will be converted to uppercase |
-n | The variable is declared as a reference for another variable |
-r | The variable is declared as read-only |
-t | The variable is declared for tracing: triggers the DEBUG trap |
-x | The variable is declared to export as an environment variable |
To unset the attribute, use the + sign with the attribute. Note that the indexed and associative array attribution cannot be unset.
Examples of declare Command in Linux
The following section demonstrates the examples of the declare command in Linux bash scripting −
- Declaring a Variable
- Declaring an Integer Variable
- Declaring an Indexed Array
- Declaring an Associative Array
- Changing the Case of a String Variable
- Declaring a Read Only Variable
- Unsetting the Attribute of a Declared Variable
Declaring a Variable
To declare a variable in a bash script, use the declare keyword followed by the variable name.
To assign a value to the variable, use the equal (=) sign.
Here, the value is directly assigned, however, it can also be done separately.
In the above declaration, no attribute is given to the variable.
To verify whether the variable has been declared or not, the -p option will be used −
declare -p
The above command will display all the declared variables on the system including the declared variable.
Use the grep command to filter out the specific declared variable −
The double dash (--) before the variable name in the output indicates that the variable has no attribute.
Declaring an Integer Variable
To declare the integer variable, the -i attribute is used −
declare -i variable variable=16
Let’s verify it using the -p option −
declare -p | grep variable
Any arithmetic operation can be performed during the variable declaration. For example, to perform addition −
Bash does not directly support floating point arithmetic. So, in case of mixed output, the decimal value will be printed.
For example, if you divide 19 by 3, only 6 will be printed instead of 6.333.
It is also important to note that -i attribution cannot be assigned to a mixed number (i.e. 1.2, 0.5).
The output shows an error.
If you try to assign a string to a variable with integer type attribution, the output will be "0".
Declaring an Indexed Array
The -a attribute is used with the declare command to declare an indexed array −
declare -a array array[0]=16
To add multiple values to an array, change the index in the above command −
array[1]=18 array[2]=19 array[3]=20
To display the assigned value at a specific index −
echo ${array[0]}
To display all the values of the indexed array, use wildcard (*) as index −
echo ${array[*]}
Declaring an Associative Array
The associative array stores elements in key-value pairs. The associative arrays have string indexes, unlike indexed arrays where indexes are integers. To attribute an array as an associative array -A is used −
declare -A array array[one]="Hello" array[two]="World"
To print the value of a specific index, use −
echo ${array[two]}
To print all values −
echo ${array[*]}
Changing the Case of a String Variable
The declare command comes with special attributes that change the case of the string variable to upper or lower case.
For instance, to change a string to uppercase, use the -u attribute −
declare -u variable="hello" echo $variable
Similarly, to change a string to lowercase, use the -l attribute −
declare -l variable="hello" echo $variable
Declaring a Read Only Variable
The declare command can also assign a variable with a read-only attribute. The read-only variable cannot be modified during the script execution.
declare -r variable=16
If you try to assign another value, a read-only error will appear −
Unsetting the Attribute of a Declared Variable
To unset a specific attribute, use the + sign is used. The uppercase attribute -u is unset with the +u attribute in the following example.
After unsetting the attribute, the variable retains its original case as shown in the above example.
Conclusion
The declare command in Linux declares variables to strictly enforce the type by giving them an attribute. By default, bash is a weakly typed scripting language. However, for better variables management they can be declared like other strongly typed programming languages using the declare command.