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
- 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
declare command works with the following general options:
|Declare a bash function, not a variable.|
|Display the function's name and attributes.|
|Apply the global scope to all the variable operations inside a shell function. The option does not work outside shell functions.|
|Display options and attributes of variables.|
The options in the table below are used to set an attribute to a variable.
|The variable is an indexed array. You cannot unset this attribute.|
|The variable is an associative array. You cannot unset this attribute.|
|The value of the variable is an integer. Unset the attribute with |
|The variable name consists of lowercase characters only. Unset the attribute with |
|The variable becomes a name reference for another variable. Unset the attribute with |
|The variable is read-only. Unset the attribute with |
|If used with functions, the item inherits |
|The variable name consists of uppercase characters only. Unset the attribute with |
|Export the variable to child processes, similar to the export command. Unset the attribute with |
How to Declare a Variable in Bash
The following example shows how to declare a variable named
testvar and assign it the value of
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
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 (
declare +x testvar
-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.
Use the echo command to see the value of the variable.
The output shows the value of zero (
The value of a variable can also be the result of a mathematical operation.
In the example above, the
testvar variable was given the value of
3*3. Checking the variable value with
echo shows the result.
The result of the mathematical operation does not have to be an integer.
However, if the variable has the
-i attribute set, the result is rounded to the closest integer.
Attempting to declare a non-integer value to a variable while the
-i option is set, results in an error.
-u attribute to convert all letters to uppercase. The example below declares the
testvar variable and gives it the value of
declare -u testvar="example"
However, since the
-u attribute was set, checking the value of the variable shows that it has the value of
-l attribute has the opposite effect.
declare -l testvar="EXAMPLE"
Setting it converts uppercase letters to lowercase.
-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.
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:
Declare more elements of the array by specifying the element's index number.
List all the elements of the array by typing:
Note: Once you declare a variable as an array, you cannot use
+a to unset the option.
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.