Bash declare Statement Syntax and Examples

The declare statement is a built-in Bash command that allows you to set attributes for variables and control their behavior. When you declare a variable using declare, you can specify how Bash should treat that variable whether it's an array, integer, read-only, or has other special properties.

Syntax of Declare Statement

The basic syntax of the declare statement is straightforward

declare [options] variable=value

Where declare is the keyword, [options] are flags that set specific attributes, and variable=value assigns a value to the variable.

Common Declare Options

Option Description Example
-a Declares variable as an indexed array declare -a fruits
-A Declares variable as an associative array declare -A colors
-i Declares variable as an integer declare -i count=10
-r Makes variable read-only declare -r constant="fixed"
-x Exports variable to environment declare -x PATH
-u Converts value to uppercase declare -u name="john"
-l Converts value to lowercase declare -l city="LONDON"

Basic Examples

Declaring Arrays

# Indexed array
declare -a fruits=("apple" "banana" "cherry")
echo "${fruits[1]}"  # Output: banana

# Associative array
declare -A colors=([apple]="red" [banana]="yellow" [cherry]="red")
echo "${colors[banana]}"  # Output: yellow

Integer Variables

declare -i num1=5
declare -i num2=10
declare -i result=num1+num2
echo "The result is: $result"  # Output: The result is: 15

Read-only Variables

declare -r config_path="/etc/myapp"
# config_path="/tmp"  # This would cause an error

Advanced Examples

Case Conversion

# Uppercase conversion
declare -u username="john_doe"
echo "$username"  # Output: JOHN_DOE

# Lowercase conversion  
declare -l city="NEW YORK"
echo "$city"  # Output: new york

Multiple Variable Declaration

declare var1=10 var2="Hello World" var3=("one" "two" "three")
echo "$var1"        # Output: 10
echo "$var2"        # Output: Hello World
echo "${var3[0]}"   # Output: one

Name References

declare original="Hello Universe!"
declare -n reference=original
echo "$reference"   # Output: Hello Universe!

# Modifying through reference
reference="Modified value"
echo "$original"    # Output: Modified value

Key Points

  • The declare command can be used both to declare new variables and modify existing ones

  • Multiple options can be combined: declare -rx PATH="/usr/bin" makes a read-only exported variable

  • Using declare -p variable displays the attributes and value of a variable

  • Variables declared with declare inside functions are local by default

  • The -g option can make variables global even inside functions

Conclusion

The declare statement is an essential Bash feature for controlling variable behavior and attributes. It enables you to create arrays, enforce data types, set read-only constraints, and manage variable scope effectively. Mastering declare makes your Bash scripts more robust and maintainable.

Updated on: 2026-03-17T09:01:38+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements