Where to start First steps track · 15 stages
Linux Labs

test, [ ] and [[ ]] — checking conditions

cheat sheet · Ubuntu 24.04 · bash 5.2 · dash 0.5 https://lab.dimsanych.ru/en/wiki/test-conditions/

Cheat sheet

  • [ -f /etc/app.conf ] && echo "file exists"
  • [ -d "$dir" ] || mkdir -p "$dir"
  • [ -z "$1" ] && { echo "Usage: $0 FILE" >&2; exit 2; }
  • [ "$answer" = yes ] strings are equal
  • [ "$count" -gt 10 ] number greater than 10
  • [ -r "$f" ] && [ -s "$f" ] readable and not empty
  • [[ $name == *.log ]] pattern, bash only
  • [[ $ver =~ ^[0-9]+\.[0-9]+$ ]] regular expression, bash only

Options

The test checks are in the table below. The full list is in help test; for [[ ]], see man bash, section CONDITIONAL EXPRESSIONS.

Loading…