Introduction

Hello! Today I’ll explain the declare command.

The declare command adds types and attributes to variables. You might think Bash is a loosely-typed language with no types, but actually you can specify types using declare.

What is the declare Command?

The declare command is a Bash built-in command for setting variable attributes and declaring variables.

You can create variables with just VAR=value, but when you want to say “this variable is integer-only!” or “make this read-only!”, you use declare.

Basic Syntax

1
declare [options] variable_name[=value]

You can also use typeset (it’s an alias for the same command).

Main Options

Option Description
-i Declare as integer type
-r Declare as read-only (constant)
-a Declare as array
-A Declare as associative array
-x Export as environment variable
-l Convert to lowercase
-u Convert to uppercase
-p Display variable attributes and values

Usage Examples

Example 1: Declare Integer Variable

1
2
3
4
declare -i count
count=10
count=count+5
echo $count

Output:

1
15

When declared as integer, trying to assign a string results in 0.

1
2
3
declare -i num
num="hello"
echo $num

Output:

1
0

Example 2: Read-Only Variable (Constant)

1
2
3
4
5
declare -r API_URL="https://api.example.com"
echo $API_URL

# Trying to change causes an error
API_URL="https://newurl.com"

Output:

1
2
https://api.example.com
bash: API_URL: readonly variable

Useful for creating constants.

Example 3: Array Declaration

1
2
3
4
5
6
declare -a fruits
fruits=("apple" "banana" "orange")

echo ${fruits[0]}
echo ${fruits[1]}
echo ${fruits[@]}  # All elements

Output:

1
2
3
apple
banana
apple banana orange

Example 4: Associative Array Declaration

1
2
3
4
5
6
7
8
declare -A user
user[name]="John Doe"
user[age]=30
user[email]="john@example.com"

echo "Name: ${user[name]}"
echo "Age: ${user[age]}"
echo "Email: ${user[email]}"

Output:

1
2
3
Name: John Doe
Age: 30
Email: john@example.com

Associative arrays are super useful. Like Python dictionaries.

Example 5: Case Conversion

1
2
3
4
5
6
7
declare -l lowercase
lowercase="HELLO WORLD"
echo $lowercase

declare -u uppercase
uppercase="hello world"
echo $uppercase

Output:

1
2
hello world
HELLO WORLD

Converts automatically.

Example 6: Check Variable Attributes

1
2
3
4
5
6
7
8
declare -i num=42
declare -r constant="fixed"
declare -a array=(1 2 3)

# Display attributes
declare -p num
declare -p constant
declare -p array

Output:

1
2
3
declare -i num="42"
declare -r constant="fixed"
declare -a array=([0]="1" [1]="2" [2]="3")

Tips & Notes

Local vs Global Variables

Using declare inside a function automatically makes it a local variable.

1
2
3
4
5
6
7
8
my_function() {
    declare local_var="local"
    global_var="global"
    echo "Inside: local_var=$local_var, global_var=$global_var"
}

my_function
echo "Outside: local_var=$local_var, global_var=$global_var"

Output:

1
2
Inside: local_var=local, global_var=global
Outside: local_var=, global_var=global

local_var is empty outside the function.

Integer Arithmetic Notes

With the -i option, arithmetic evaluation happens automatically.

1
2
3
4
5
6
7
8
declare -i x=10
x=x+5    # No need for $((...))
echo $x

# With normal variables
y=10
y=y+5    # Becomes string concatenation
echo $y

Output:

1
2
15
y+5

Combining Multiple Attributes

1
2
3
4
5
# Read-only array
declare -ar readonly_array=(1 2 3)

# Exported integer
declare -ix exported_num=42

Practical Usage

Loading Configuration

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
declare -A config

load_config() {
    config[app_name]="MyApp"
    config[version]="1.0.0"
    config[debug]="true"
    config[max_connections]=100
}

load_config

echo "App: ${config[app_name]}"
echo "Version: ${config[version]}"
echo "Debug: ${config[debug]}"
echo "Max Connections: ${config[max_connections]}"

Defining Constants

1
2
3
4
5
6
7
8
9
# Application constants
declare -r APP_NAME="MyApplication"
declare -r VERSION="2.0.0"
declare -r CONFIG_DIR="/etc/myapp"
declare -r LOG_DIR="/var/log/myapp"

echo "Starting: $APP_NAME v$VERSION"
echo "Config: $CONFIG_DIR"
echo "Logs: $LOG_DIR"

Counter Processing

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
declare -i counter=0
declare -i total=0

for file in *.txt; do
    counter+=1
    lines=$(wc -l < "$file")
    total+=lines
done

echo "Files: $counter"
echo "Total lines: $total"

Managing User Data

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
declare -A users

add_user() {
    local username=$1
    local email=$2
    users[$username]=$email
}

get_user_email() {
    local username=$1
    echo "${users[$username]}"
}

add_user "alice" "alice@example.com"
add_user "bob" "bob@example.com"

echo "Alice's email: $(get_user_email alice)"
echo "Bob's email: $(get_user_email bob)"

Batch Environment Variable Setup

1
2
3
4
5
6
7
8
# Development environment setup
declare -x NODE_ENV="development"
declare -x PORT=3000
declare -x DB_HOST="localhost"
declare -x DB_PORT=5432

# All are exported
env | grep -E "NODE_ENV|PORT|DB_"

Difference Between declare and export

Command Purpose Scope
declare Set variable attributes Current shell only
declare -x Set as environment variable Inherited by child processes
export Set as environment variable Inherited by child processes

export VAR=value means the same as declare -x VAR=value.

Summary

Key points about the declare command:

  • Can set types and attributes for variables
  • -i for integer, -r for constant, -a for array, -A for associative array
  • -l for lowercase, -u for uppercase auto-conversion
  • Automatically creates local variables inside functions
  • typeset works too (alias)

When writing serious Bash scripts, using declare reduces bugs and improves readability!