if and for in Bash

Conditions and loops, so a script can decide and repeat.

Also called: for loop, if [ ], test

Source
General
Language
Bash

Example

A short for loop converted all 200 photos to WebP.

In code

for f in *.jpg; do
  cwebp -q 80 "$f" -o "${f%.jpg}.webp"
done

if [ -f .env ]; then echo "env exists"; else cp .env.example .env; fi

Learn more

Related terms

Added 2026-11-30 · Last reviewed 2026-11-30