BASH Programming

Bash Declare Command

Bash is a weekly-typed programming language that does not require to declare the data type of the variable at the time of declaration like other programming languages, such as C, C++, Java, C#, etc. But this feature can be implemented by using the declare command of the bash script. It is used to declare the shell variable and function. It has many options to declare different types of variables and functions with different attributes. The uses of this command in the bash script have been described in this tutorial.

Syntax:

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

This command can be used with an option or without an option to declare one or more variables. The option of this command is used to define the type of the variable.

Different Options of Declare Command

The purposes of using some useful options of the declare command:

Option Purpose
-i It is used to declare an integer variable.
-a It is used to declare an indexed array.
-A It is used to declare an associative array.
-l It is used to declare a variable to store all lowercase letters.
-u It is used to declare a variable to store all uppercase letters.
-p It is used to print the attributes and options of the variables.
-x It is used to export the variable.
-f It is used to declare a bash function.
-F It is used to print the name of the function and attributes.
-n It is used to declare a variable that references another variable.

Different Examples of Declare Command:

Example-1: Use of Declare Command Without an Option

The ‘–’ option is used with the declare command for using declare command without no option. Create a bash file with the following script that uses declare command to declare a variable without any option and with the ‘–‘ option. The first declare command has been used without any option or value. The second declare command has been used with the ‘–‘ option and without value. The third declare command has been used without any option and with a value.

#!/bin/bash

#Declare a variable without a value

declare var1

#Declare a variable without the option and value

declare -- var2

#Declare a variable with the value

declare var3="Hello"

#Print the declared variables

printf "First variable:$var1\nSecond variable:$var2\nThird variable:$var3\n"

Output:

The following output will appear after executing the above script.

Example-2: Use of Declare Command -i Option

Create a bash file with the following script that uses declare command with the -i option to declare an integer variable. If the number value is taken from the terminal, then the input value will be stored in the variable. Otherwise, 0 will be stored in the variable.

#!/bin/bash

#Declare an integer variable

declare -i intvar

#Take input from the user

read -p "Enter a number: " intvar

#Print the input value

printf "The number is $intvar\n"

Output:

The following output will appear after taking the numeric value the first time and the string value the second time.

Example-3: Use of Declare Command -a Option

Create a bash file with the following script that uses declare command with –a option to declare an array variable. The array values will be taken from the terminal and stored in the array variable. Next, the values of the array will be printed by using for loop.

#!/bin/bash

#Declare an array variable

declare -a arrvar

#Take the inputs for the array variables

read -p "Enter the array values: " arrvar

printf "\nArray values are:\n"

#Read the array values

for val in ${arrvar[@]}

do

echo $val

done

Output:

The following output will appear after taking the array values.

Example-4: Use of Declare Command -r Option

Create a bash file with the following script that uses declare command to declare a variable with the –r option and to declare a read-only variable with a value. The read-only variable will not be re-assigned again.

#!/bin/bash

#Declare a read-only variable with the value

declare -r strvar="Hello"

#Print the variable

echo "The value of the read-only variable is $strvar"

#Re-assign the value to the variable

strvar=" World"

Output:

The following output will appear after executing the above script. The output shows that the variable is read-only.

Example-5: Use of Declare Command -l Option

Create a bash file with the following script that uses declare command to declare a variable with a string value of all capital letters and the –l option:

#!/bin/bash

#Declare a variable with the value

declare -l strvar="HELLO WORLD"

#Print the variable

echo "The value of the variable is $strvar"

Output:

The following output will appear after executing the above script.

Example-6: Use of Declare Command -u Option

Create a bash file with the following script that uses declare command to declare a variable with a string value of all small letters and the –u option:

#!/bin/bash

#Declare a variable with the value

declare -u strvar="linuxhint"

#Print the variable

echo "The value of the variable is $strvar"

Output:

The following output will appear after executing the above script:

Example-7: Use of Declare Command -p Option

Create a bash file with the following script that shows the use of declare command with the –p option. The first declare command has been used to declare a variable without any option and with a value. The second declare command has been used to declare a variable with the -i option and with a numeric value. The third declare command has been used with the –p option to print all declared variables and attributes.

#!/bin/bash

#Declare a variable with the value

declare strvar="linuxhint"

#Declare an integer variable with the value

declare -i intvar=25

#Print the list of all variables, attributes, and values

declare -p

Output:

The following similar output will appear after executing the above script:

Conclusion

The uses of different options of the declare command have been explained in this tutorial by using multiple examples that will help the bash user to know the purpose of using the declare command properly.

About the author

Fahmida Yesmin

I am a trainer of web programming courses. I like to write article or tutorial on various IT topics. I have a YouTube channel where many types of tutorials based on Ubuntu, Windows, Word, Excel, WordPress, Magento, Laravel etc. are published: Tutorials4u Help.