The declare Command in Bash

Nilesh Katuwal Feb 03, 2022
  1. the Syntax of declare Command Using Bash
  2. Options as General Parameters of declare Command in Bash
  3. the Type of Integers Used in declare Command in Bash
  4. Declare ReadOnly Variable in Bash
  5. Use the Lowercase and Uppercase Conversion When Assigning a Variable in Bash
  6. Use the Subshells to Export Variables in Bash
  7. Check if Any attributes Have Been Specified or Not in Bash
  8. Check Function Definition in Bash
The declare Command in Bash

Bash employs attributes that can be set by a command to allow type-like behavior, as the bash type system is not robust.

The declare is a built-in Bash command that allows you to change the characteristics of variables within your shell’s scope.

It can also make a longhand declaration of a variable. Finally, it gives you access to variables.

The declare -A creates an associative array variable, an array of key-value pairs whose values are indexed by a keyword.

The Bash functions can be given attributes in addition to variables that affect their behavior.

the Syntax of declare Command Using Bash

$ declare [-a] [-A] [-f] [-F] [-g] [-i] [-l] [-n] [-r]
        [-t] [-u] [-x] [-p] [name[=value]] [name[=value]] ...

Options as General Parameters of declare Command in Bash

The declare inbuilt command accepts the following options as general parameters:

$ bash -c "help declare"

Output:

declare: declare [-aAfFgilnrtux] [-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.
    
    Options:
      -f	restrict action or display to function names and definitions
      -F	restrict display to function names only (plus line number and
    		source file when debugging)
      -g	create global variables when used in a shell function; otherwise
    		ignored
      -p	display the attributes and value of each NAME
    
    Options which set attributes:
      -a	to make NAMEs indexed arrays (if supported)
      -A	to make NAMEs associative arrays (if supported)
      -i	to make NAMEs have the `integer` attribute
      -l	to convert the value of each NAME to lower case on assignment
      -n	make NAME a reference to the variable named by its value
      -r	to make NAMEs `readonly`
      -t	to make NAMEs have the `trace` attribute
      -u	to convert the value of each NAME to upper case on assignment
      -x	to make NAMEs export
    
    Using `+` instead of `-` turns off the given attribute.
    
    Variables with the integer attribute have arithmetic evaluation (see
    the `let` command) performed when the variable is assigned a value.
    
    When used in a function, `declare` makes NAMEs local, as with the `local`
    command. The `-g` option suppresses this behavior.
    
    Exit Status:
    Returns success unless an invalid option is supplied or a variable
    assignment error occurs.

Here are some examples of help commands to see how they appear in your terminal. It’s worth noting that the final one is a fail-safe for our Windows Git Bash users.

Now that you’ve read the introduction and manual page for bash declare, it’s time to look at some real-world examples using bash declare.

the Type of Integers Used in declare Command in Bash

Use the -i flag to declare a variable as an integer. You can see that the variable age is specified as an integer type in the example below.

When you assign different type strings values, you might expect an error message that says Not the correct type; rather than throwing an error, the error variable will be set to zero.

$ declare -i num=7
$ echo $num

Output:

7

Reassigning the integer type to a string yields 0 as a result.

$ num=string
$ echo $num

Output:

0

Declare ReadOnly Variable in Bash

This coding word describes how a variable is given a value and cannot be modified by the programmer or the machine. It stays the same throughout the program’s lifespan.

Use the -r flag to make your variable constant read-only.

$ declare -r num=7

Output:

bash: declare: num: readonly variable

As you can see, the output result says readonly variable.

Use the Lowercase and Uppercase Conversion When Assigning a Variable in Bash

When assigning a variable to bash, you can use the -l lowercase and -u uppercase flags to change it from lowercase to uppercase and vice versa.

$ declare -l name="THANOS"
$ declare -u name1="thanos"
$ echo $name $name1

Output:

thanos THANOS

Use the Subshells to Export Variables in Bash

If you’ve used bash before, you’ve probably noticed people utilizing the export command to export declared variables to subshells in their scripts or shell sessions. We can do the same thing with the declare command.

The -x flag should be used to export, while the +x flag will prevent the attribute from being exported.

$ declare -x name=thor
$ sh -c "echo $name"

Output:

thor

Check if Any attributes Have Been Specified or Not in Bash

Using the -p flag, we can see if an attribute variable is defined or not.

$ a=5;b=6;c=7
$ declare -p a b c

Output:

declare -- a="5"
declare -- b="6"
declare -- c="7"

After specifying the keyword, there will be two dashes --. Declare -a array. Declare -n number. It is used to show the variable’s type nameref.

If no declarations are made, it will just display.

Check Function Definition in Bash

The -F and -f flags can be used to check if the function is declared and defined. I’m making a simple inevitable_thanos function.

$ function inevitable_thanos(){ echo "ironman"; }
$ declare -f

Output:

inevitable_thanos ()
{ 
    echo "ironman"
}
quote ()