Introduction
A variable in bash is created by assigning a value to its reference. Although the built-in declare
statement does not need to be used to explicitly declare a variable in bash, the command is often employed for more advanced variable management tasks.
This tutorial will show you how to work with variables and their attributes using the bash declare
command.
Prerequisites
- Access to the terminal/command line.
- Sudo user privileges.
Bash Declare Syntax
The syntax for using the bash declare
command is:
declare [options] [variable-name]="[value]"
Note: The system also accepts passing the value without quotation marks.
Bash declare Options
The declare
command works with the following general options:
Option | Description |
---|---|
-f | Declare a bash function, not a variable. |
-F | Display the function's name and attributes. |
-g | Apply the global scope to all the variable operations inside a shell function. The option does not work outside shell functions. |
-p | Display options and attributes of variables. |
The options in the table below are used to set an attribute to a variable.
Option | Description |
---|---|
-a | The variable is an indexed array. You cannot unset this attribute. |
-A | The variable is an associative array. You cannot unset this attribute. |
-i | The value of the variable is an integer. Unset the attribute with +i . |
-l | The variable name consists of lowercase characters only. Unset the attribute with +l . |
-n | The variable becomes a name reference for another variable. Unset the attribute with +n . |
-r | The variable is read-only. Unset the attribute with +r . |
-t | If used with functions, the item inherits DEBUG and RETURN traps from the parent shell. Unset the attribute with +t . |
-u | The variable name consists of uppercase characters only. Unset the attribute with +u . |
-x | Export the variable to child processes, similar to the export command. Unset the attribute with +x . |
How to Declare a Variable in Bash
The following example shows how to declare a variable named testvar
and assign it the value of 100
.
declare testvar="100"
When successfully executed, the command produces no output. Use the -p
option to check if the variable was successfully created. Since the command lists all the variables currently present on the system, limit it with the grep command.
declare -p | grep testvar
The double dash (--
) sign preceding the variable name signifies that the variable has no arguments.
To set an attribute to a variable, use the syntax below. The example sets the -x
attribute to the testvar
variable.
declare -x testvar
Search for the variable again and confirm that the attribute has been set.
declare -p | grep testvar
Note: You can also search for the variable by passing its name after the -p
option. For example:
declare -p testvar
To remove the attribute, precede it with a plus (+
) sign.
declare +x testvar
Integers
The -i
option limits the variable to integer values only.
declare -i testvar
To test this, attempt to change the variable's value to a string of characters.
declare testvar="example"
Use the echo command to see the value of the variable.
echo $testvar
The output shows the value of zero (0
):
The value of a variable can also be the result of a mathematical operation.
declare testvar="3*3"
In the example above, the testvar
variable was given the value of 3*3
. Checking the variable value with echo
shows the result.
echo $testvar
The result of the mathematical operation does not have to be an integer.
declare testvar="10/3"
However, if the variable has the -i
attribute set, the result is rounded to the closest integer.
echo $testvar
Attempting to declare a non-integer value to a variable while the -i
option is set, results in an error.
declare testvar="1.5"
Cases
Use the -u
attribute to convert all letters to uppercase. The example below declares the testvar
variable and gives it the value of example
.
declare -u testvar="example"
However, since the -u
attribute was set, checking the value of the variable shows that it has the value of EXAMPLE
.
echo $testvar
The -l
attribute has the opposite effect.
declare -l testvar="EXAMPLE"
Setting it converts uppercase letters to lowercase.
echo $testvar
Read-Only
Set the -r
attribute to the variables you want to protect against accidental change.
declare -r testvar="100"
Attempting to change the value of the read-only variable results in an error.
declare testvar="50"
Arrays
Bash variables can have more than one value. To assign multiple values to a single bash variable, convert it to an array by typing:
declare -a testvar
If the variable had a value before conversion, that value is now the first element of the array, with the index number 0
. To check the value of the first element in the variable array, type:
echo ${testvar[0]}
Declare more elements of the array by specifying the element's index number.
declare testvar[1]="200"
List all the elements of the array by typing:
echo ${testvar[*]}
Note: Once you declare a variable as an array, you cannot use +a
to unset the option.
Conclusion
After completing this tutorial you should be able to use the bash declare
command to declare variables and set/unset their attributes.
To edit the default bash prompt, read How to Customize Bash prompt in Linux.