&&
lets you do something based on whether the previous command completed successfully. It may be used to chain commands, so that the next command runs only if the previous succedded.
If this is Your goal, there is a simpler way. If You want to run the next command only if the previous one succedded, You may want to use:
set -euo pipefail
If any of the commands fail (return with a non-zero status), than the script will stop.
From http://tldp.org/LDP/abs/html/options.html :
- set -e -> Abort script at first error, when a command exits with non-zero status (except in until or while loops, if-tests, list constructs)
- set -u -> Attempt to use undefined variable outputs error message, and forces an exit
- set -o pipefail -> Causes a pipeline to return the exit status of the last command in the pipe that returned a non-zero return value.
( Every shell script that i write, starts with a set -euo pipefail
. It is a good practice. )