How do you use arrays in bash?

Answer

Bash supports indexed arrays (0-based). Declare: arr=(one two three) or declare -a arr. Access: ${arr[0]} (first element), ${arr[@]} (all elements), ${arr[*]} (all as one string). Length: ${#arr[@]}. Append: arr+=(four). Modify: arr[1]="TWO". Iterate: for item in "${arr[@]}"; do echo "$item"; done. Slice: ${arr[@]:1:2} (elements 1 and 2). Delete element: unset arr[2]. Array from command: files=($(ls *.txt)) — but better: mapfile -t files < <(ls *.txt) to handle filenames with spaces safely.