Skip to content
Learn Netverks

Lesson

Step 21/36 58% through track

arrays-bash

Arrays in Bash

Last reviewed May 28, 2026 Content v20260528
Track mode
none
Means
Read / quiz
Reading
~1 min
Level
intermediate

This lesson

This lesson teaches Arrays in Bash: the syntax, patterns, and safety habits you need before advancing in Bash.

Teams still ship Arrays in Bash in Bash codebases—skipping it leaves gaps in debugging and code reviews.

You will apply Arrays in Bash in contexts like: CI jobs, server maintenance, local dev automation, and Git hooks.

Read each lesson, copy bash examples into your own terminal, and complete the lesson MCQs—there is no in-browser runner for security reasons.

When you can explain the previous lesson's ideas without copying starter code.

Indexed arrays hold ordered lists; associative arrays (Bash 4+) map keys to values. Arrays are Bash-specific—avoid them when you need pure POSIX sh.

Indexed arrays

fruits=("apple" "banana" "cherry")
echo "first: ${fruits[0]}"
echo "all: ${fruits[@]}"
echo "count: ${#fruits[@]}"

${#arr[@]} is length; indices start at 0.

Append and iterate

nums=()
nums+=(1)
nums+=(2 3)
for n in "${nums[@]}"; do
  echo "n=$n"
done

Always quote "${arr[@]}" to preserve separate elements.

Associative arrays

declare -A capitals
capitals[UK]="London"
capitals[FR]="Paris"
echo "${capitals[UK]}"

Requires Bash 4+—check bash --version on macOS if features fail.

Important interview questions and answers

  1. Q: 0-based arrays?
    A: Yes in Bash indexed arrays—unlike R's 1-based vectors.
  2. Q: declare -A?
    A: Creates an associative (hash) array.

Self-check

  1. How do you get array length?
  2. Why quote ${arr[@]} in loops?

Tip: macOS default Bash 3.2 lacks associative arrays—check version before declare -A.

Interview prep

Indexed array access?

${arr[0]} first element.

declare -A?

Associative array (Bash 4+).

Interview tip Lesson completion confidence

Can you explain this lesson in 30 seconds without reading notes?

Not saved yet.

Check yourself

Multiple choice — immediate feedback.

Discussion

Past discussion is visible to everyone. Only logged-in users can post comments and replies.

Starter discussion topics

  • Indexed vs assoc?
  • ${arr[@]} why?

Sign up or log in to post comments and sync lesson progress across devices.

No discussion yet. Be the first to ask a question.

Jump