Interview › Scripting (Bash, Groovy)
How do you declare and iterate over an array in Bash? [Intermediate]
Answer
A Bash indexed array is declared with parentheses, such as arr=(a b c). I iterate with "${arr[@]}" to preserve each element exactly.
Technical explanation
Arrays are Bash-specific and are not portable to plain POSIX sh.
Use "${array[@]}" for elements and ${!array[@]} for indexes.
Quote each element expansion to handle spaces safely.
Hands-on example
services=("orders api" "payments" "worker")
for svc in "${services[@]}"; do
echo "deploying [$svc]"
done
services+=("scheduler")
echo "total=${#services[@]}"Preparing for an interview?
Check how well your resume matches the role with our free resume checker— match score, ATS check, and the skills you're missing.
More Scripting (Bash, Groovy) interview questions
- What is an associative array, and how do you use one? [Intermediate]
- How do you split a string on a delimiter in Bash? [Intermediate]
- What is the difference between glob expansion and regex in the shell? [Intermediate]
- How do you check whether a command exists before using it? [Intermediate]
- What is the difference between NR and NF in awk? [Advanced]
- What is shellcheck, and what kinds of issues does it catch? [Intermediate]
- How do you use awk to sum or average a column? [Advanced]
- What are exit codes you should use, and what does a non-zero exit signal to a pipeline? [Intermediate]