Skip to content

Control Flow ​

Because Eshell commands can not (easily) be combined with Lisp forms, Eshell provides command-oriented control flow statements for convenience.

Most of Eshell’s control flow statements accept a conditional. This can take a few different forms. If conditional is a dollar expansion, the condition is satisfied if the result is a non-nil value. Alternately, conditional may be a subcommand, either in command form, e.g. ‘{subcommand}’; or in Lisp form, e.g. ‘(lisp form)’. In that case, the condition is satisfied if the subcommand’s exit status is 0.

if conditional true-subcommand ​

if conditional true-subcommand else false-subcommand ​

Evaluate true-subcommand if conditional is satisfied; otherwise, evaluate false-subcommand. Both true-subcommand and false-subcommand should be subcommands, as with conditional.

You can also chain together if/else forms, for example:

bash
if {[ -f file.txt ]} {
  echo found file
} else if {[ -f alternate.txt ]} {
  echo found alternate
} else {
  echo not found!
}

unless conditional false-subcommand ​

unless conditional false-subcommand else true-subcommand ​

Evaluate false-subcommand if conditional is not satisfied; otherwise, evaluate true-subcommand. Like above, you can also chain together unless/else forms.

while conditional subcommand ​

Repeatedly evaluate subcommand so long as conditional is satisfied.

until conditional subcommand ​

Repeatedly evaluate subcommand until conditional is satisfied.

for var in sequence… subcommand ​

Iterate over each element of sequence, storing the element in var and evaluating subcommand. If sequence is a range of the form begin..end, iterate over each integer between begin and end, not including end. If sequence is not a sequence, treat it as a list of one element.

If you specify multiple sequences, this will iterate over each of them in turn.