bash which OR operator to use – pipe v double pipe

| isn’t an OR operator at all. You could use ||, though:

which ansible || {
  true # put your code to install ansible here
}

This is equivalent to an if:

if ! which ansible; then
  true # put your code to install ansible here
fi

By the way — consider making a habit of using type (a shell builtin) rather than which (an external command). type is both faster and has a better understanding of shell behavior: If you have an ansible command that’s provided by, say, a shell function invoking the real command, which won’t know that it’s there, but type will correctly detect it as available.

Leave a Comment