Home Linux Commands How to Use Declare Command in Linux Bash Shell

How to Use Declare Command in Linux Bash Shell

In this article, we will be looking at what is the use of the declare command in bash and how to use it. A declare is a bash built-in command that provides type-like behavior for your scripts.

If you have any prior experience in any programming language like Java, C you may know what is a type of system. Sadly, bash does not have a strong type system. With declare command, you can get type-like behavior and you can set attributes for your variables.

$ type -a declare

declare is a shell builtin

We will go through some of the useful features declare offers. A good place to start and get to know about declare is either through help command or bash manpage.

$ help declare

declare: declare [-aAfFgiIlnrtux] [-p] [name[=value] ...]
    Set variable values and attributes.
    Declare variables and give them attributes.  If no NAMEs are given,
    display the attributes and values of all variables.
      -f	restrict action or display to function names and definitions
      -F	restrict the display to function names only (plus line number and
    		source file when debugging)
      -g	create global variables when used in a shell function; otherwise
      -I	if creating a local variable, inherit the attributes and value
    		of a variable with the same name at a previous scope
      -p	display the attributes and value of each NAME

How to Declare Integers Type

To declare a variable to be of type integer use -i flag. If you look at the below example you can see variable age is declared to be an integer type. When you try and assign values of another type (strings) you may expect it to return an error saying “Not the correct type” but here instead of throwing the error variable will be assigned as zero (0). Yes, this is strange behavior and not what one expects it to be.

$ declare -i num=10
$ echo $num
$ num=string     # Reassigning Interger type with string
$ echo $num      ⇒ Zero will be the output
Declaring Integer Type
Declaring Integer Type

How to Declare Readonly Variables

Readonly – This is a programming term meaning a variable is assigned with a value and never can be changed either by the programmer or by machine. It remains the same throughout the lifecycle of the program.

To make your variable constant (read-only) use the -r flag.

$ declare -r num=10
$ num=11
-bash: no: readonly variable
Declaring ReadOnly Variable
Declaring ReadOnly Variable

An alternate method to make a variable read-only is to use the readonly keyword.

$ readonly os=ubuntu
$ os=arch
-bash: os: readonly variable
readonly command to make variable
readonly command to make variable

Lowercase and Uppercase Conversion in Linux

When assigning a variable to bash you can convert it from lower to uppercase and vice versa using the -l (lowercase) and -u (uppercase) flag.

$ declare -l name="LINUX"
$ declare -u name1="linux"
$ echo $name $name1
Upper to Lower Case Conversion
Upper to Lower Case Conversion

How to Declare Indexed / Associative Array

Bash supports two types of arrays. Indexed arrays and associative array. To declare arrays use -a (Indexed array) and -A (associative array) flag. You can create an indexed array without using declare command but to create an associative array you must use declare command with -A flag.

$ declare -a os_name=(Ubuntu Mint Kubuntu lubuntu Debian)
$ echo ${os_name[@]}
$ declare -A os_family
$ os_family["Redhat"]="Fedora"
$ os_family["Arch"]="Manjaro"
$ os_family["Debian"]="Ubuntu"
$ echo ${os_family[@]}
Indexed and Associative Array
Indexed and Associative Array

Exporting Variables to Subshells

If you have some experience with bash you might or seen people using the export command to export declared variables to subshells in their script or shell sessions. We can do that using declare command too.

To export, you should use the -x flag and +x flag will remove the attribute from exporting.

$ declare -x name=ShellTips
$ sh -c “echo $name”
Exporting Variable
Exporting Variable

Check If Attributes are Defined or Not

We can check if an attribute (variable) is defined or not using the -p flag.

# Defining a variable
$ a=10;b=20;c=30
# Check if a variable is defined or not
$ declare -p a    # single variable check
declare -- a="10"
$ declare -p a b c   # check multiple variables
declare -- a="10"
declare -- b="20"
declare -- c="30"

You will see two dashes -- after declaring keyword. This is to display the type of the variable like declare -a (array), declare -n (nameref). If nothing is declared it will just display --.

$ declare -a Iarray
$ declare -A arrray
$ declare -p Iarray Asarray

declare -a Iarray
declare -A Asarray

If you try to find an undeclared variable following error will be thrown.

$ declare -p Iarray1
-bash: declare: Iarray1: not found
Attribute Check
Attribute Check

How to Check Function Definition

You can use the -F and -f flag to see if the function is declared and function definition. I am creating a simple hello world function.

$ function hello_world(){ echo "Linux Geeks"; }
$ declare -F

To get the function definition you can use the -f flag. If no function name is passed it will print all the loaded functions and you can get your function definition from the list.

$ declare -f hello_world
$ declare -f
Function Definition
Function Definition

That’s it for this article. We will catch with another interesting article very soon.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

1 thought on “How to Use Declare Command in Linux Bash Shell”

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.