Bash declare Statement: Syntax and Examples

December 23, 2021

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.

Bash declare Statement: Syntax and Examples.

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:

OptionDescription
-fDeclare a bash function, not a variable.
-FDisplay the function's name and attributes.
-gApply the global scope to all the variable operations inside a shell function. The option does not work outside shell functions.
-pDisplay options and attributes of variables.

The options in the table below are used to set an attribute to a variable.

OptionDescription
-aThe variable is an indexed array. You cannot unset this attribute.
-AThe variable is an associative array. You cannot unset this attribute.
-iThe value of the variable is an integer. Unset the attribute with +i.
-lThe variable name consists of lowercase characters only. Unset the attribute with +l.
-nThe variable becomes a name reference for another variable. Unset the attribute with +n.
-rThe variable is read-only. Unset the attribute with +r.
-tIf used with functions, the item inherits DEBUG and RETURN traps from the parent shell. Unset the attribute with +t.
-uThe variable name consists of uppercase characters only. Unset the attribute with +u.
-xExport 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.

Checking the value of the variable with declare -p.

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
Checking if the attribute was set properly with declare -p.

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 integer variable interprets the string output as having the value of zero.

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
Checking the variable that was assigned the value expressed as a mathematical calculation.

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
Illustrating the rounding property of the integer variable.

Attempting to declare a non-integer value to a variable while the -i option is set, results in an error.

declare testvar="1.5"
The error that appears when a non-integer value is passed to an integer variable.

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 -u attribute converting all the lowercase letters to uppercase.

The -l attribute has the opposite effect.

declare -l testvar="EXAMPLE"

Setting it converts uppercase letters to lowercase.

echo $testvar
The -l attribute converting all the uppercase letters to lowercase.

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"
Attempting to change a read-only variable.

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]}
Checking the value of the first element in the variable array.

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[*]}
Displaying all the elements in the variable array.

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.

Was this article helpful?
YesNo
Marko Aleksic
Marko Aleksić is a Technical Writer at phoenixNAP. His innate curiosity regarding all things IT, combined with over a decade long background in writing, teaching and working in IT-related fields, led him to technical writing, where he has an opportunity to employ his skills and make technology less daunting to everyone.
Next you should read
How To Run A Bash Script
December 9, 2021

Scripts store commands that often go together, such as updates and upgrades, to accomplish certain tasks automatically. Follow this tutorial to learn how to run a Bash script using various methods.
Read more
Bash trap Command Explained
December 16, 2021

A script that fails may leave behind temporary files that cause trouble when a user restarts the script. This tutorial will show you how to use the trap command to ensure your scripts always exit predictably.
Read more
Bash case Statement
December 15, 2021

The case statement tests the input value until it finds the corresponding pattern and executes the command linked to that input value. Learn the bash case statement basics and how to use it in shell scripts.
Read more
Bash if elif else Statement
October 21, 2021

The if elif else statement in bash scripts allows creating conditional cases and responses to specific code results. This article explains what the if elif else statement is and shows the syntax through various examples.
Read more