Just to be clear on this -
YES, you can join two or more commands together using the
&&
(
AND) operator.
But you should be aware that it is a conditional operation.
The operations are processed from left to right -
BUT - the right-hand command after an && will
ONLY be executed if the command to its left succeeds.
And in a Unix-like system, "success" is indicated by a return-value of 0. Any value other than 0 indicates failure.
So the command to the right of an && operator only gets executed if the command to the left returns 0.
Bearing that in mind - consider this:
Bash:
echo "This prints!" && echo "So does this!" && false && echo "What about this?!"
Which yields the following output:
Code:
This prints!
So does this!
The final string "What about this?" is not shown....
Why is this? All of the commands were chained together using &&......
Let's see:
The first command echo's to the screen, returns 0 - fulfilling the criteria for the &&/AND operator - so the second command is executed.
The second command echo's to the screen, returns 0 - and the third command is executed.
The third command is
false
(which always returns 1, or false) so because of this - the final echo command does not get ran.
So - yes, you can combine commands using &&, but it is a conditional operator.
Similarly, there is the || (
OR) operator.
With the OR operator, the operation on the right will
only be ran if the operation on the left
fails.
e.g.
Bash:
echo "Hello!" || echo "Goodbye!"
Yields:
Hello!
Now let's simulate the first call to echo failing by calling false instead, just so you can see it working:
Yields:
Goodbye!
Now, that might not seem particularly useful in those last two examples. However, you could use || (OR) to catch the failure of false in my original snippet and print something else:
e.g.
Code:
echo "This prints!" && echo "So does this!" && false && echo "What about this?!" || echo "Ooops, call to false failed!"
Which yields:
Code:
This prints!
So does this!
Ooops, call to false failed!
So in the above, when false failed - the echo statement after the && (AND)
does not run, but the echo command after the || (OR)
does run.
Typically, if there's a chance that part of your script could fail - you can use the || (OR) operator to catch that failure and log an error, or perform an alternative action instead.
Also, you can combine && (AND) and || (OR) instead of
if... then else....
type syntax in your scripts.
e.g.
Something like this:
Bash:
if [[ -f /path/to/file ]] ; then
echo "File exists!"
else
echo "File does not exist!"
fi
Can also be written like this:
Bash:
[[ -f /path/to/file ]] && echo "File exists!" || echo "File does not exist!"
Above you have 5 lines of if..then..else condensed down to a single line, using && (AND) and || (OR). Both snippets do exactly the same thing when they are ran - they are no different.
The first version is definitely more human-readable. But the second is shorter and more concise/succinct.
Anyway - I've gone slightly off-topic. But that's the basics of using && (AND) and || (OR) to combine several commands.
The main thing to remember about them is that they are conditional! If you want to unconditionally run several commands at once in a single line, you can simply separate them with a semicolon
;
instead!