FUNDAMENTALS A Complete Guide for Beginners
A variable is a standby where you assign some value for later use for your code. The declare command in Linux bash scripts is used to declare shell variables and functions. It sets their attributes and displays their values. In this article, I’ll discuss how you can use the declare command in Linux.
A. Description
The declare command in Linux is a shell built-in command that provides type-like behavior for the bash scripts. Bash is not a strongly typed programming language and it does not require the declaration of the data type of the variable during declaration like other languages. But with the declare command in bash scripts, we can get type-like behavior.
B. Syntax
The syntax of the declare command is quite simple. Just type the command with option or no option and then assign variables with values. Use the equal (=) sign when assigning values to variables. You can use multiple options and assign values to multiple variables at the same time.
Declare [OPTIONS] [name[=value]]...
C. Options
There are several options for the declare command. And it has some options that are used to set attributes to variables. I will list some of the most useful ones with examples here. As it is a shell built-in, the declare command does not have the man page. But you can get info using the help command to read more about them.
help declare
Useful Options
- -p (to display the attributes & options of the variable)
- -f (to declare a bash function)
- -F (to print the name of the function & attributes)
Options that set attributes
- -i ( to declare an integer value)
- -l (to declare a variable to convert to all lowercase letters)
- -u (to declare a variable to convert to all uppercase letters)
- -a (to declare an indexed array)
- -A (to declare an associative array)
- -n ( to declare a variable that references another variable)
- -x (to export the variable)
- -r (to make attributes read-only)
Practical Examples of the “declare ” Command in Linux
The declare command is very useful in bash scripts. We can use the command to declare different types of variables & functions with different attributes. We can add some useful options with the command to modify the usage. Below I will discuss some of the most useful options with a list of practical examples where you can see how to use the declare command in different aspects.
Example 1: Use the “declare” Command to Declare a Variable in Linux
To declare a variable, just type declare and then the variable name. You can assign any value to the variable using the equal (=) sign. Keep the assigned value of the variable inside a double quotation (“ ”). The simple syntax to declare a variable is:
declare variable_name="value"
I will create a variable named var. To do so you can follow the below process:
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
declare var
➌ To assign value for the variable type the following command
var=05
➍ Type the following command to view the assigned value of the variable.
echo "the value of the variable is $var"
➎ Now, press the ENTER button.
Output >
From the output, you will see the value of the variable is printed out.
Example 2: Use the “declare” Command to Assign an Integer Variable
You can assign an integer value to the variable. To do that use the declare command with option -i to declare an integer variable. The syntax is:
declare -i variable_name="integer_value"
I will show you how to use it with some examples. Follow the below steps.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt ( to check what happens when you use option -i & don’t assign any integer value ).
declare -i number
➌ To assign value for the variable type the following command
number="Hi"
➍ Type the following command to view the assigned value of the variable.
echo $number
➎ Press the ENTER button.
➏ Now, type the following command ( to check if your variable takes a fraction number as a value when you are using the -i option).
declare -i number="1.5"
➐ Press the ENTER button.
➑ Then, type the following commands (to check some arithmetic operations using the -i option)
declare -i multiply="5*5"
echo $multiply
➒ Press ENTER & then type the below commands
declare -i divide="9/2"
echo $divide
➓ Finally type ENTER again.
Output >
From the output image, you can see for the first command, when no integer number is assigned as a value the output of that variable is “0” ( by default).For the second command, when you will use a fraction number as a value it will output an error. You can from the below picture.With the third and fourth commands, you can check some arithmetic operations. But from the below output image you can see, the output will always be only an integer value. It does not display the fraction part of any function.
Example 3: Use the “declare” Command in Linux to Assign an Array
Bash variables can have multiple values. To set an attribute to a variable as an array, use the declare command with option -a. The syntax is:
declare -a array_name[n]="value"
Where “n” is the element number of the array.
I will show you an example. To do so yourself, follow the below steps.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
declare -a array
➌ Check if your array had any assigned value beforehand. For the first element of the array, the value of “n” will be “0”. Type the following command,
echo ${array[0]}
➍ Declare element values for the array. Type these commands,
declare array[0]="A"
declare array[1]="B"
declare array[2]="C"
declare array[3]="D"
declare array[4]="D"
➎ Now, type the following command to view all the values of the array variable.
echo ${array[*]}
➏ Finally, press the ENTER button.
Output >
From the output image, you can see first I declared an array & checked whether it has any assigned value as the first element or not. I didn’t get any output because my array did not have any pre-assigned value. After that, I declared element values for my array. I have 5 elements(0-4) in there. At last, I displayed my array element values using the echo command.
- The “alias” Command in Linux [3 Practical Examples]
- The “unalias” Command in Linux [3 Practical Examples]
- The “exit” Command in Linux [5 Practical Examples]
Example 4: Convert All String Variable Letters to Uppercase
You can declare a variable with a string of all capital letters. And you can do so even if you are using letters of both cases while assigning the string. To do so use the -u option with the declare command. The syntax is:
declare -u variable_name="string"
Follow the below example to check.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
declare -u data="linux"
➌ Type the following command to view the assigned value of the variable.
echo "This is a code language of $data"
➍ Now, press the ENTER button.
Output >
From the below picture, you can see the output string value is all in uppercase letters.
Example 5: Convert All String Variable Letters to Lowercase
You can declare a variable with a string of all small letters too. And you can do so even if you are using letters of both cases or all capital letters while assigning the string. To do so use the -l option with the declare command. The syntax is:
declare -l variable_name="string"
Follow the below example to check.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
declare -l name="SOFTEKO"
➌ Type the following command to view the assigned value of the variable.
echo $name
➍ Now, press the ENTER button.
Output >
From the below picture, you can see the output string value is all in lowercase letters.
Example 6: Use the “declare” Command in Linux to Set a Read-Only Variable
Suppose, you want to declare a variable and you don’t want to change it anymore, to do so use the -r option with the declare command. This way you will declare a read-only variable. The read-only variables will not be re-assigned. If you try to re-assign, the output will show you that the variable is read-only. The syntax is:
declare -r variable_name="value"
Follow the below example to check.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Type the following command in the command prompt:
declare -r pi="3.14159"
➌ Now try to re-assign the pi variable by typing the following command.
declare -r pi="50"
➍ Finally, press the ENTER button.
Output >
From the below picture, you can see the output is telling you that the pi variable is read-only.
Example 7: PrintOut the Options and Attributes of the Variables
You can see all the declared variables and their attributes by using the option -p with the declare command. It will show you all the variables in your machine along with your running shell variables at the bottom. You can also display the attribute & name of a particular variable by giving the variable name after the -p option. If that particular variable had no value or option with it, we will see a double dash (–) with the output. The syntax is:
declare -p Variable_name
Check the below example. Follow the steps to do it yourself.
Steps to Follow >
➊ At first open the Ubuntu Terminal.
➋ Declare a variable with no option. Type the following command in the command prompt:
declare rainbow="7 colors"
➌ Press ENTER.
➍ Type the following command to display the variable.
declare -p rainbow
➎ Now, declare a variable with an option. Type the following command in the command prompt:
declare -i count="200"
➏ Then, type the following command to display the variable.
declare -p count
➐ Now, press the ENTER button.
➑ To check all the variables and their attributes, type the following command.
declare -p
➒ Finally, press ENTER.
Output >
See from the below picture, using the first command I declared a variable named rainbow without any option. I used the second command to display my declared variable and in the output, we can see the name and attribute of the variable rainbow followed by the double dash(–) sign. The sign tells us that no options were used while declaring the variable. In the third command, I declared another variable named count, where I used -i as an option. To display the count variable I used the fourth command. From the output, we can see the variable name with the option & attribute and no double dash.After running the declare -p command to display all the variables & their attributes, we will see the below images as output. Where we can see all the machine variables.And all of the running shell variables at the bottom.
Conclusion
In this article, I discussed how to declare variables in bash scripts. I used the declare command with different options & further discussed their usage with practical examples. If you go through all the examples, I hope you will learn all about assigning variables with different types of attributes.
Related Articles
- The “bash” Command in Linux [5 Practical Examples]
- The “clear” Command in Linux [3 Practical Examples]
- The “echo” Command in Linux [7 Practical Examples]
- The “export” Command in Linux [8 Practical Examples]
- The “source” Command in Linux [3 Practical Examples]
- The “hash” Command in Linux [6 Practical Examples]
- The “read” Command in Linux [8 Practical Examples]
- The “set” Command in Linux [6 Practical Examples]
- The “unset” Command in Linux [2 Practical Examples]
- The “eval” Command in Linux [4 Practical Examples]