The “declare” Command in Linux [7 Practical Examples]

LINUX
FUNDAMENTALS
A Complete Guide for Beginners Enroll Course Now

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]]...
Note: In the above syntax variable name and assigned value are enclosed by a square bracket and followed by 3 dots representing that you can assign multiple at the same time.

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)
Note: The options in Linux CLI (Command Line Interface) are all case-sensitive, So be cautious while using them.

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.

Note: Normally the echo command displays the words you put inside the double quotation (“ “ ) as an argument. But when you are using the dollar ($) sign with any word, it will display its value.

Output >

From the output, you will see the value of the variable is printed out.Assigning variable with the declare command in linux.

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).Declaring integer variable in Linux.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.Assigning fraction number for integer variable with declare command in linux.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.Arithmetic operation done using -i option with declare command.

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.Declaring array variable in linux.



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.Uppercase conversion os string array.

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.Lowercase conversion os string variable.

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.Declaring read-only variable using declare command in linux.

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.Displaying variables using declare command with -p option.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.Displaying all variables in machine.And all of the running shell variables at the bottom.Displaying all variables in running shell.

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

Rate this post
Monira Akter Munny

Hello!! This is Monira Akter Munny. I'm a Linux content developer executive here, at SOFTEKO company. I have completed my B.Sc. in Engineering from Rajshahi University of Engineering & Technology in the Electrical & Electronics department. I'm more of an online gaming person who also loves to read blogs & write. As an open-minded person ready to learn & adapt to new territory, I'm always excited to explore the Linux world & share it with you! Read Full Bio

Leave a Comment