For Loop in Shell

Level 365 keystrokes

for loop iterates over a list of items and executes commands for each.

Shell code

# *.txt expands to the matching names in alphabetical order; the loop runs once per file
# If nothing matches, the loop runs once with the literal *.txt
1for file in *.txt; do
2 echo "Processing: $file"
# Quotes keep names with spaces intact; wc -l prints the line count and the name
3 wc -l "$file"
4done

Output

Processing: notes.txt
      25 notes.txt
Processing: readme.txt
      10 readme.txt
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.

  • "× 4
  • $× 2
  • -× 1
  • ;× 1
  • :× 1
  • .× 1
  • *× 1

More Shell examples at level 3

← Shell typing practice