bash which OR operator to use – pipe v double pipe
When I’m looking at bash script code, I sometimes see | and sometimes see || , but I don’t know which is preferable.
I’m trying to do something like ..
Please advise which OR operator is preferred in this scenario.
2 Answers 2
| isn’t an OR operator at all. You could use || , though:
This is equivalent to an if :
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.
There is a big difference between using a single pipe (pipe output from one command to be used as input for the next command) and a process control OR (double pipe).
This runs the cat command on the /etc/issue file, and instead of immediately sending the output to stdout it is piped to be the input for the less command. Yes, this isn’t a great example, since you could instead simply do less /etc/issue – but at least you can see how it works
For this one, the touch command is run, or attempted to run. If it has a non-zero exit status, the double pipe OR kicks in, and tries to execute the echo command. If the touch command worked, then whatever the other choice is (our echo command in this case) is never attempted.
bash which OR operator to use – pipe v double pipe When I’m looking at bash script code, I sometimes see | and sometimes see || , but I don’t know which is preferable. I’m trying to do