Array Basics in Shell

Level 358 keystrokes

Arrays store multiple values and can be iterated with for loops.

Shell code

# Creates an indexed array of five elements (bash)
1nums=(10 20 30 40 50)
# ${nums[@]} expands to every element; quoting it ("${nums[@]}") keeps elements with spaces whole
2for n in ${nums[@]}; do
# Prints one value per line: 10 to 50
3 echo $n
4done

Output

10
20
30
40
50
Practice typing this code

Practice typing this code. Results are not recorded in the rankings.

Symbols in this code

The symbols you type in this Shell code and how often they appear.

  • $× 2
  • ;× 1
  • (× 1
  • )× 1
  • [× 1
  • ]× 1
  • {× 1
  • }× 1
  • @× 1
  • =× 1

More Shell examples at level 3

← Shell typing practice