Conditionals

Conditionals let you branch variable definitions based on values resolved earlier in the same <!-- cheat --> block.

Syntax

if $var == value
  var name := something
fi

if $var != value
  var name = some shell command
fi

Any variable form works inside an if block: prompt-only, shell (=), and literal (:=).

How evaluation works

The condition is evaluated after $var is resolved. CheatMD substitutes the variable’s value and compares:

  • == - true if the substituted value equals value
  • != - true if the substituted value does not equal value

If the condition is false, all var definitions inside the block are skipped.

Example: branching on auth method

var auth = printf 'key\npassword\n' --- --header "Auth method"

if $auth == key
  var ssh_flags := -o PreferredAuthentications=publickey
fi

if $auth == password
  var ssh_flags := -o PreferredAuthentications=password
fi

When the user picks “key”, $ssh_flags gets -o PreferredAuthentications=publickey. The password block is skipped entirely.

Example: environment-specific URLs

var env = printf 'dev\nstaging\nprod\n' --- --header "Environment"

if $env == dev
  var url := https://api.dev.example.com
fi

if $env == staging
  var url := https://api.staging.example.com
fi

if $env == prod
  var url := https://api.example.com
fi

Nesting

Conditionals do not nest. Each if / fi pair is independent. Use multiple sequential blocks to handle complex branching.

All-conditional variables

If a variable is only defined inside conditional blocks and none of the conditions match, the variable is silently set to an empty string and skipped during resolution. This prevents dangling prompts for variables that don’t apply.

Linting

The Linting validates conditional syntax:

  • Mismatched if without fi
  • Missing $ prefix on the condition variable
  • Invalid operators (only == and != are supported)

See also