What is “declare” in Bash?

A variable in a bash script contains a numeric value, a character, and a string of characters. There is no need to declare the variable data type compared to other programming languages like C, C++, C#, and Java. But this useful feature can be implemented using the “declare” command in a bash script. It sets the shell function and the variable with various options to declare the variable data type based on their name and functionality. 

This guide illustrates the purpose, functionality, and practical implementation of bash’s “declare” command.

How Does “declare” Command Work in Bash?

The working of the “declare” command relies on its basic syntax, which is provided below:

Syntax:

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

The above syntax contains the following parameters:

  • declare: represents the “declare” command line tool.
  • options: shows supported flags of the “declare” command.
  • name: refers to the name of the declared variable.
  • value: identify the value of the variable, either numeric or string, etc.

 To explore different options of the “declare” command, use the “help” option as below:

$ declare --help

Let’s see how these options set the attributes in the bash script.

Example 1: Declare a Variable Without Argument

The “declare” variable use “–(double dash)” that indicates no argument is passed with the defined variable. Following “Program.sh” script code shows its practical implementation:

#!/bin/bash
declare -- var2=8
echo "The variable value is:$var2"

The above lines of code are described here:

  • The “#! /bin/bash” tells the script to be executed in the “bash” shell.
  • A “declare” command uses the “” operator with a variable “var2” to assign the “2” value.
  • Lastly, the “echo” command prints the argument alongside the “$var2” value.

Press “Ctrl+S” to save and “Ctrl+X” to exit the editor. 

Make the “Program.sh” script executable using the “chmod” command:

$ sudo chmod +x Program.sh

The script is now ready to execute.

Output:

The “Program.sh” is executed in the command line:

$ ./Program.sh

The variable “var2” value has been displayed in the terminal.

Example 2: Declare Integer Variable

The “-i(integer)” argument is used with the “declare” command to declare an integer variable. It accepts a numeric value. 

Let’s see how it works:

#!/bin/bash
declare -i num=79
echo "The num value is=$num"

The “-i” argument with the “declare” command denotes that the “num” variable stores an integer value “79”. The “echo” command will print the “$num” integer value:

Output:

The output of the “New.sh” script can be displayed in the terminal by executing the below command:

$ ./New.sh

The output has successfully printed the “79” value of the “num” variable in the terminal.

Example 3: Declare Array Variable

The “-a(array)” flag declares that the defined variable nature is “array”. Create/open the “Code.sh” script to check its functionality:

#!/bin/bash
declare -a arr=("35" "45" "78" "99" "57")
echo "The array list contains following values :${arr[@]}"

In the above code, the “arr” array variable is utilized with the “a” option that contains numeric values “35″ “45” “78” “99” “57”. In the second line, the “@” symbol is used in the “{arr[@]}” variable that prints all values defined in the “arr” array list:

Output:

Run the script to get the above “Code.sh” output in the terminal:

$ ./Code.sh

The array “arr” list values have been printed successfully.

Example 4: Declare Uppercase String

The “declare” command allows us to specify the string variables in uppercase using the “-u(uppercase)” flag. Follow the example to perform this task:

#!/bin/bash
declare -u case=ITSLINUXFOSS
echo "The defined string is $case"

In the above script, the “case” variable is declared as having string “ITSLINUXFOSS” in uppercase.

Output:

Run the “Sam.sh” script and check its output:

$ ./Sam.sh

The “case” variable value has been printed as “ITSLINUXFOSS” in the command line.

Example 5: Declare Lowercase String

Same as uppercase the user can assign the string value in “lowercase”. It can be done by utilizing the “-l(lowercase)” argument with the “declare” command. Let’s see how it works:

#!/bin/bash
declare -l str=ubuntu
echo "The lowercase string is $str"

In the above code snippet, the “str” variable contains the string value in lowercase “ubuntu” that is printed through the “echo” command.

Output

The “Ex.sh” bash script execution shows the following output:

$ ./Ex.sh

The “ubuntu” lowercase string has been shown in the command line.

Example 6: Declare Readable Variable

The “-r(readable)” flag is used to declare the “read-only” variable having a value “Welcome”. 

Create/open a bash script to check its practical implementation:

#!/bin/bash
declare -r read=Welcome
echo "The readable variable value is:$read"read=Hello

In the above script file, the first “read” variable stores the “Welcome” string. The second “read” variable again takes another new string, “Hello”, to check whether it will assign or not.

Output

The execution of the “Script.sh” shows the below results:

$ ./Script.sh

The output shows that the new string is not assigned to the read” variable because it is only a read-only variable declared by the “-r” flag.

Example 7: Print Declared Variables

The “-p” flag of the “declare” command prints all the declared variables in the terminal. The following script example shows how this task can be performed:

#!/bin/bash
declare -- var2=8
declare -i num=79declare -a arr=("35" "45" "78" "99" "57")declare -u case=ITSLINUXFOSSdeclare -l str=ubuntudeclare -l str=ubuntudeclare -p

In the above block multiple variables are declared that include “integer”, “array”, “read-only”, “uppercase string”, “lowercase string” datatype:

Run the “Imp.sh” script:

$ ./Imp.sh

The output shows all the variables declared in the bash scripts alphabetically.

Conclusion

In the bash script, the “declare” command sets the variable and functions alongside their attributes, i.e., data type. The date type includes the “integer”, “string (uppercase and lowercase)”, “array”, “readonly” variable, and so on. 

This guide has explained the “declare” command and different bash functionality.