2024-07-10
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.
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.
declare
accepts many options to modify variable behavior:
-i
(integer): Declares an integer variable. Arithmetic operations are automatically handled.declare -i count=0
count=$((count + 1))
echo "$count" # Output: 1
Attempting to assign a non-integer value will result in an error.
-r
(readonly): Creates a read-only variable. Attempts to modify its value after declaration will fail.declare -r PI=3.14159
PI=3 # This will result in an error
-x
(export): Exports the variable to the environment of subsequently executed processes.declare -x MY_EXPORTED_VAR="exported value"
./my_script.sh # my_script.sh can access MY_EXPORTED_VAR
-a
(array): Creates an array variable.declare -a my_array=("apple" "banana" "cherry")
echo "${my_array[0]}" # Output: apple
echo "${my_array[@]}" # Output: apple banana cherry
-A
(associative array): Creates an associative array (key-value pairs), available in Bash 4 and later.declare -A my_assoc_array
my_assoc_array["fruit"]="apple"
my_assoc_array["color"]="red"
echo "${my_assoc_array[fruit]}" # Output: apple
-l
(lowercase): Converts the value to lowercase.declare -l my_string="HeLlO"
echo "$my_string" # Output: hello
-u
(uppercase): Converts the value to uppercase.declare -u my_string="HeLlO"
echo "$my_string" # Output: HELLO
Multiple attributes can be combined:
declare -ir counter=0 # Integer and read-only
declare
and Functionsdeclare
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.
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.