Skip to content
Bloutiouf edited this page Oct 13, 2011 · 3 revisions

Operators bind commands together, manipulating arguments and outputs of each two operands. Each operator has its own way to execute commands, they are explained in sections below.

There is a priority with operators. Use parenthesis to force a certain order of execution. From greatest to lowest priority, the operators are:

Operator Symbol Forwards arguments to Gives outputs of
Join & both operands both operands
Execution : left operand right operand
Pipe | left operand right operand
And && left operand left or right operand
Or || left operand left or right operand
Insertion ~ left operand both operands
Union ; left operand both operands
Separator ;; left operand right operand

Which means that these two lines are equivalent, because && has a greater priority than ||:

confirm "Drink coffee?" && echo "Ok, lets drink!" || echo "Back to work..."
(confirm "Drink coffee?" && echo "Ok, lets drink!") || echo "Back to work..."

By the way, this is a handy way to make a conditional operator: condition && if_true || if_false.

The left operand is always processed before the right operand.

Join operator &

The join operator forwards outputs it receives to its both operands, and gives them outputs back.

> list hello world | echo & count
hello world
2

See also the union operator ;.

Execution operator :

The execution operator delimits execution scope. For now, it can only be used with command foreach as left operand.

> list hello world | foreach i : echo \"$i!\" | echo
"hello!" "world!"

Pipe operator |

The pipe operator gives output of the left operand as arguments to the right operand.

> list hello world | count
2

See also the insertion operator ~.

And operator &&

The and operator executes the left operand. If it produces no output, gives no output back. Otherwise, executes the right operand and gives its output.

> confirm && echo "You have confirmed."

See also the or operator ||.

Or operator ||

The and operator executes the left operand. If it produces output, gives this output back. Otherwise, executes the right operand and gives its output.

> confirm || echo "You have canceled."

See also the and operator &&.

Insertion operator ~

The insertion operator executes the left operand, then executes the right operand giving output of the first operand as arguments, and returns both outputs.

> list hello world ~ count
hello
world
2

See also the pipe operator |.

Union operator ;

The union operator gives output of both operands, but it forwards only its arguments to the left operand.

> set var foo bar ; echo $var ; count $var
foo bar
2

See also the join operator & and the separator operator ;;.

Separator operator ;;

The separator operator executes both operands but only gives output of the right operand.

> set var foo bar ;; echo $var ;; count $var
2

See also the union operator ;.

Clone this wiki locally