Exit Status in Shell

Level 385 keystrokes

Exit status indicates success (0) or failure (non-zero) of commands.

Shell code

# -q prints nothing; if tests grep's exit status: 0 (a match) means true
# If log.txt is missing, grep returns 2 and the else branch runs too
1if grep -q "error" log.txt; then
2 echo "Errors found!"
# Ends the script with status 1 to signal failure to the caller
3 exit 1
4else
# Runs when no line contains error; prints: All clear
5 echo "All clear"
6fi

Output

All clear
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.

  • "× 6
  • -× 1
  • ;× 1
  • !× 1
  • .× 1

More Shell examples at level 3

← Shell typing practice