Arrays in Bash
Bash supports two types of arrays: indexed arrays (numbered) and associative arrays (key-value pairs). While not as flexible as arrays in Python or JavaScript, they're indispensable for many scripting tasks.
Indexed Arrays
bash
# Create an array
fruits=("apple" "banana" "cherry" "date")
# Access elements (0-indexed)
echo "${fruits[0]}" # apple
echo "${fruits[2]}" # cherry
# Access all elements
echo "${fruits[@]}" # apple banana cherry date
# Array length
echo "${#fruits[@]}" # 4
# Add an element
fruits+=("elderberry")
# Modify an element
fruits[1]="blueberry"
# Remove an element
unset fruits[3]
# Loop over an array
for fruit in "${fruits[@]}"; do
echo "Fruit: $fruit"
done
# Loop with index
for i in "${!fruits[@]}"; do
echo "Index $i: ${fruits[$i]}"
doneAssociative Arrays
Associative arrays use strings as keys. You must declare them with declare -A.
bash
# Declare an associative array
declare -A colors
colors[red]="#FF0000"
colors[green]="#00FF00"
colors[blue]="#0000FF"
# Or inline
declare -A user=(
[name]="Alice"
[age]="30"
[role]="admin"
)
# Access by key
echo "${user[name]}" # Alice
# All keys
echo "${!user[@]}" # name age role
# All values
echo "${user[@]}" # Alice 30 admin
# Loop
for key in "${!colors[@]}"; do
echo "$key = ${colors[$key]}"
done
๐ก Tip: Associative arrays require Bash 4.0 or later. Check with
bash --version. macOS ships with Bash 3 by default โ install a newer version via Homebrew if needed.
Array Operations
bash
arr=(one two three four five)
# Slice โ ${array[@]:offset:length}
echo "${arr[@]:1:3}" # two three four
# String length of element
echo "${#arr[0]}" # 3 (length of "one")
# Replace in all elements
echo "${arr[@]/o/O}" # One twO three fOur fiveString Manipulation
Bash has powerful built-in string operations using parameter expansion:
bash
path="/home/user/documents/report.txt"
# Remove shortest match from front
echo "${path#*/}" # home/user/documents/report.txt
# Remove longest match from front
echo "${path##*/}" # report.txt (basename!)
# Remove shortest match from end
echo "${path%/*}" # /home/user/documents (dirname!)
# Remove longest match from end
echo "${path%%/*}" # (empty โ removed everything after first /)
# Substring replacement
str="hello world world"
echo "${str/world/Bash}" # hello Bash world (first occurrence)
echo "${str//world/Bash}" # hello Bash Bash (all occurrences)
# Substring extraction
str="Hello, World!"
echo "${str:7:5}" # World
# Uppercase / lowercase (Bash 4+)
str="hello"
echo "${str^^}" # HELLO
echo "${str^}" # Hello (first char only)
str="HELLO"
echo "${str,,}" # hello
echo "${str,}" # hELLO (first char only)
# Default values
echo "${undefined_var:-default}" # prints "default"
echo "${undefined_var:=default}" # sets AND prints "default"Arithmetic
bash
# $(( )) for arithmetic
result=$((5 + 3))
echo $result # 8
# All standard operators
echo $(( 10 - 3 )) # 7
echo $(( 4 * 5 )) # 20
echo $(( 20 / 3 )) # 6 (integer division)
echo $(( 20 % 3 )) # 2 (modulo)
echo $(( 2 ** 10 )) # 1024 (exponentiation)
# Increment/decrement
count=0
((count++))
((count += 5))
echo $count # 6
โ ๏ธ Warning: Bash arithmetic is integer only. For floating-point math, use
bc: echo "scale=2; 10/3" | bc outputs 3.33.
Try It Yourself
Terminal
Summary
You've learned indexed and associative arrays, slicing and looping arrays, powerful string manipulation with ${} expansions, and arithmetic with $(( )). These advanced variable techniques are essential for serious Bash scripting.