declare

2024-07-10

Understanding declare

The declare built-in command is used to declare variables and specify their attributes. This goes beyond simple variable assignment; it allows you to explicitly define the type and scope of a variable, enhancing both script readability and functionality.

Basic Variable Declaration

At its simplest, declare functions similarly to a standard variable assignment:

declare my_variable="Hello, world!"
echo "$my_variable"  # Output: Hello, world!

This creates a string variable. However, declare’s true power lies in its options.

Specifying Variable Attributes

declare accepts many options to modify variable behavior:

declare -i count=0
count=$((count + 1))
echo "$count"  # Output: 1

Attempting to assign a non-integer value will result in an error.

declare -r PI=3.14159
PI=3   # This will result in an error
declare -x MY_EXPORTED_VAR="exported value"
./my_script.sh  # my_script.sh can access MY_EXPORTED_VAR
declare -a my_array=("apple" "banana" "cherry")
echo "${my_array[0]}"  # Output: apple
echo "${my_array[@]}"  # Output: apple banana cherry
declare -A my_assoc_array
my_assoc_array["fruit"]="apple"
my_assoc_array["color"]="red"
echo "${my_assoc_array[fruit]}"  # Output: apple
declare -l my_string="HeLlO"
echo "$my_string" # Output: hello
declare -u my_string="HeLlO"
echo "$my_string" # Output: HELLO

Combining Attributes

Multiple attributes can be combined:

declare -ir counter=0 # Integer and read-only

declare and Functions

declare is also useful within functions to control the scope of variables:

my_function() {
  local my_local_var="local variable"
  declare -g global_var="global variable" # Makes the variable global
  echo "$my_local_var"
  echo "$global_var"
}

my_function
echo "$my_local_var" # This will not print anything, as the variable is local to the function
echo "$global_var"   # This will print "global variable"

The local keyword is a shorthand for declare -l. Using declare -g within a function makes a variable global, otherwise, variables are local by default.

Advanced Usage and Examples

The flexibility of declare opens doors to more complex scripting. Consider a scenario where you need to handle different data types dynamically:

data_type="integer"
if [[ "$data_type" == "integer" ]]; then
  declare -i my_var=10
else
  declare my_var="string"
fi

This demonstrates how declare can be integrated with conditional logic for more adaptive scripts. The possibilities extend further with careful combination of declare with other shell features.