You may know that you can chain commands at the terminal. So instead of writing
$ echo 'foo' $ echo 'bar'
And hitting return after each one, you could type:
$ echo 'foo' && echo 'bar'
rm -rf node_modules && npm i && say done
That’s a freebie.
But this is not the only way to chain commands 😮 You can also use semi-colons So what’s the difference?
$ alpha() && beta() # beta() only executes if alpha() is successful $ alpha(); beta() # beta() executes even if alpha() fails
What’s the difference between a successful and unsuccessful command in *nix? The exit code of course!
0exit code = good ✔️
1or higher exit code = bad ✖️
An exit code of anything greater than
0 means your command failed.
&& for my example above is good as I don’t want the rest of the commands in the chain to execute unless the preceding ones pass.
If you’re familiar with logical operators then this should make sense to you. If you’re not, then go google logical operators.
false && true is false but
false || true is true.
;to execute all commands in a chain
- logical-and operator
&&to ensure the previous commands have executed successfully first
I use the Bash shell in iTerm on my Mac. 💻