'&&' vs. '&' with the 'test' command in Bash

Active3 hr before
Viewed126 times

8 Answers


What is & in Bash? And a single & means that the preceding commands—to the immediate left of the &—should simply be run in the background.,The meaning of && and & are intrinsically different.,What is && in Bash? In Bash—and many other programming languages—&& means “AND”. And in command execution context like this, it means items to the left as well as right of && should be run in sequence in this case.,Since the second command is just running test -x examples.desktop in the background it happens quite quickly, so the process ID is spawned and gone pretty immediately.

So looking at your example:

gndlp @ubuntu: ~$ test - x examples.desktop && echo $ ?
   gndlp @ubuntu : ~$ test - x examples.desktop & echo $ ? [1] 2992
load more v

command2 will execute if command1 has executed successfully. This operator allows us to check the exit status of command1.,The second command will only execute if the first command has executed successfully i.e, its exit status is zero. This operator can be used to check if the first command has been successfully executed. This is one of the most used commands in the command line.,The execution of the second command is independent of the exit status of the first command. If the first command does not get successfully executed, then also the second command will get executed.,Even if the exit status of the first command is 1 i.e, b already exists, the second command will get executed and b will be again initialized with a new value.


command1 && command2
load more v

"&&" is used to chain commands together, such that the next command is run if and only if the preceding command exited without errors (or, more accurately, exits with a return code of 0)., Stack Exchange network consists of 178 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers. ,"-" is a command line argument with no specific bash function. Exactly how it is handled is dependent on the command being run (in this case apt-key). Depending on context, it's typically used to indicate either "read data from stdin rather than from a file", or "process the remainder of the line as data rather than as command line arguments".,&& Can also be used inside of the built-in test command in bash [[ ]] to combine expressions in a similar fashion as combining commands. The entire test operation will only be true if both operands (sides) of && are true, for example:

"\" by itself at the end of a line is a means of concatenating lines together. So the following two lines:

gpg--keyserver keyserver.ubuntu.com--recv 26 C2E075 && \
export --armor 26 C2E075

are processed exactly the same as if the line was written as the single line:

gpg--keyserver keyserver.ubuntu.com--recv 26 C2E075 && gpg--
export --armor 26 C2E075
load more v

The double brackets allow you to put the && and || between the tests themselves. This is a newer syntax and may not be supported by every shell.,The && and || operators can be useful in a number of ways and, hopefully, are a little less mysterious than they might have seemed.,The Unix shell’s && and || operators provide some very useful functionality, but they can be a bit mysterious, especially considering the number of options for how they are used.,This script will output a "c" twice if the first argument ia "a" or if the second is "b". It will do the same thing if the first two arguments are "a" and "b".


echo $1 $2

if [$1 == "a"] && [$2 == "b"]
echo c

if [
   [$1 == "a" && $2 == "b"]
echo c
load more v

Is my PHP login system following best practices? Is the code really OOP? ,I am trying to understand how the logical operator precedence works in bash. For example, I would have expected, that the following command does not echo anything., Unix & Linux help chat ,Making statements based on opinion; back them up with references or personal experience.

So what happens in your example is that the leftmost operation (||) is carried out first:

true || echo aaa

Since true is obviously true, the || operator short-circuits and the whole statement is considered true without the need to evaluate echo aaa as you would expect. Now it remains to do the rightmost operation:

(...) && echo bbb

Since the first operation evaluated to true (i.e. had a 0 exit status), it's as if you're executing

true && echo bbb

You would get the same behavior with

false && echo aaa || echo bbb

It seems that in C and C-like languages && has higher precedence than || which is probably why you expected your original construct to behave like

true || (echo aaa && echo bbb).
load more v

The TEST-COMMAND list is executed, and if its return status is zero, the CONSEQUENT-COMMANDS list is executed. The return status is the exit status of the last command executed, or zero if no condition tested true.,The following example demonstrates that TEST-COMMANDS might be any UNIX command that returns an exit status, and that if again returns an exit status of zero:,The [ (or test) built-in evaluates conditional expressions using a set of rules based on the number of arguments. More information about this subject can be found in the Bash documentation. Just like the if is closed with fi, the opening square bracket should be closed after the conditions have been listed.,See the info pages for Bash for more information on pattern matching with the "(( EXPRESSION ))" and "[[ EXPRESSION ]]" constructs.

anny~ > cat msgcheck.sh

echo "This scripts checks the existence of the messages file."
echo "Checking..."
if [-f /
   var / log / messages
echo "/var/log/messages exists."
echo "...done."

anny~ > . / msgcheck.sh
This scripts checks the existence of the messages file.
   /var/log / messages exists.

load more v

There must be spaces between operators and expressions. For example, 2+2 is not correct; it should be written as 2 + 2.,Unix / Linux - Basic Operators,Unix / Linux - Shell Loops,Unix / Linux - Shell Functions


val = `expr 2 + 2`
echo "Total value : $val"
load more v

The most obvious situation where you might need to quote a string is with the echo command, which just takes its arguments and prints them to the standard output. What is the point of this? As you will see in later chapters, the shell does quite a bit of processing on command lines—most of which involves some of the special characters listed in Table 1.6. echo is a way of making the result of that processing available on the standard output.,Here is a more practical example of quoting special characters. A few UNIX commands take arguments that often include wildcard characters, which need to be escaped so the shell doesn’t process them first. The most common such command is find, which searches for files throughout entire directory trees.,[17] This should also teach you something about the flexibility of placing I/O redirectors anywhere on the command line—even in places where they don’t seem to make sense.,the result is the string, taken literally. You needn’t quote the entire line, just the portion containing special characters (or characters you think might be special, if you just want to be sure):

$ echo 2 * 3 > 5 is a valid inequality.
$ echo '2 * 3 > 5 is a valid inequality.'
$ echo '2 * 3 > 5'
is a valid inequality.
load more v

Other "command-undefined" queries related to "'&&' vs. '&' with the 'test' command in Bash"