typestar

Arrays in Bash

Real lists, if you quote them right.

langs=(bash python rust)
langs+=(go)

echo "count: ${#langs[@]}"
echo "first: ${langs[0]}, last: ${langs[-1]}"

# "${arr[@]}" expands one word per element -- the quoting matters
for lang in "${langs[@]}"; do
    echo "- $lang"
done

How it works

  1. (a b c) builds; += appends; ${#langs[@]} counts.
  2. [-1] reads from the end.
  3. "${langs[@]}" expands one word per element — the quotes matter.

Keywords and builtins used here

The run, in numbers

Lines
10
Characters to type
227
Tokens
51
Three-star pace
75 tpm

At the three-star pace of 75 tokens a minute, this run takes about 41 seconds.

Type this snippet

Step 1 of 3 in Arrays, step 10 of 26 in Language basics.

← Previous Next →

Arrays in other languages