Skip to content

Bash If / Else

Conditional logic lets scripts take different paths based on tests.

Example

#!/bin/bash
FILE="example.txt"
if [[ -f "$FILE" ]]; then
  echo "Found $FILE"
else
  echo "Creating $FILE"
  echo "Hello" > "$FILE"
fi

How to run it

  1. Save it to a file.
  2. Run chmod +x filename.sh where applicable.
  3. Run it from the terminal.

← Back homeRun in Sandbox →