Confusing use of && and || operators












72















I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I'm studying for an exam), and I've become a bit confused about the && and the || operators. I've read where they can be used in statements such as:



if [ test1 ] && [ test2 ]; then
echo "both tests are true"
elif [ test1 ] || [ test2 ]; then
echo "one test is true"
fi


However, this script shows single line statements such as:



[ -z "$SMQUEUE" ] && SMQUEUE="QUEUE"
[ -f /usr/sbin/sendmail ] || exit 0


These seem to be using the && and || operators to elicit responses based on tests, but I haven't been able to dig up documenation regarding this particular use of these operators. Can anyone explain what these do in this particular context?










share|improve this question





























    72















    I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I'm studying for an exam), and I've become a bit confused about the && and the || operators. I've read where they can be used in statements such as:



    if [ test1 ] && [ test2 ]; then
    echo "both tests are true"
    elif [ test1 ] || [ test2 ]; then
    echo "one test is true"
    fi


    However, this script shows single line statements such as:



    [ -z "$SMQUEUE" ] && SMQUEUE="QUEUE"
    [ -f /usr/sbin/sendmail ] || exit 0


    These seem to be using the && and || operators to elicit responses based on tests, but I haven't been able to dig up documenation regarding this particular use of these operators. Can anyone explain what these do in this particular context?










    share|improve this question



























      72












      72








      72


      33






      I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I'm studying for an exam), and I've become a bit confused about the && and the || operators. I've read where they can be used in statements such as:



      if [ test1 ] && [ test2 ]; then
      echo "both tests are true"
      elif [ test1 ] || [ test2 ]; then
      echo "one test is true"
      fi


      However, this script shows single line statements such as:



      [ -z "$SMQUEUE" ] && SMQUEUE="QUEUE"
      [ -f /usr/sbin/sendmail ] || exit 0


      These seem to be using the && and || operators to elicit responses based on tests, but I haven't been able to dig up documenation regarding this particular use of these operators. Can anyone explain what these do in this particular context?










      share|improve this question
















      I was skimming through an /etc/rc.d/init.d/sendmail file (I know this is hardly ever used, but I'm studying for an exam), and I've become a bit confused about the && and the || operators. I've read where they can be used in statements such as:



      if [ test1 ] && [ test2 ]; then
      echo "both tests are true"
      elif [ test1 ] || [ test2 ]; then
      echo "one test is true"
      fi


      However, this script shows single line statements such as:



      [ -z "$SMQUEUE" ] && SMQUEUE="QUEUE"
      [ -f /usr/sbin/sendmail ] || exit 0


      These seem to be using the && and || operators to elicit responses based on tests, but I haven't been able to dig up documenation regarding this particular use of these operators. Can anyone explain what these do in this particular context?







      bash shell scripting control-flow






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 16 '11 at 22:16









      Gilles

      542k12810961615




      542k12810961615










      asked Nov 16 '11 at 2:30









      Joshc1107Joshc1107

      6461813




      6461813






















          6 Answers
          6






          active

          oldest

          votes


















          116














          The right side of && will only be evaluated if the exit status of the left side is zero (i.e. true). || is the opposite: it will evaluate the right side only if the left side exit status is non-zero (i.e. false).



          You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise.



          Examples:



          $ false && echo howdy!

          $ true && echo howdy!
          howdy!
          $ true || echo howdy!

          $ false || echo howdy!
          howdy!


          Extra notes:



          If you do which [, you might see that [ actually does point to a program! It's usually not actually the one that runs in scripts, though; run type [ to see what actually gets run. If you wan to try using the program, just give the full path like so: /bin/[ 1 = 1.






          share|improve this answer





















          • 6





            When you see "X or Y", you test X. If it's true, you already know the answer to "X or Y" (it's true), so no need to test Y. If it's true, you don't know the answer to "X or Y", so you do need to test Y. When you see "X and Y", you test X. If it's false, you already know the answer to "X and Y" (it's false), so no need to test Y. If X is true, then you do need to test Y to see if "X and Y" is true.

            – David Schwartz
            Nov 16 '11 at 3:41






          • 2





            Instead of "if the left side returns zero", I would write "if the left side command's exit status is zero". I find "return" a bit ambiguous as the output and the exit status can both be considered as "return values"

            – glenn jackman
            Nov 16 '11 at 14:36











          • Not only can you "consider [ ... ] to be a program", in many cases it is. It's commonly in the file /bin/[ or /usr/bin/[.

            – L S
            Oct 28 '16 at 16:39






          • 4





            C programmers will find this super confusing. There 0 means false... While in shellscripting 0 means true... Mind blown.

            – Calmarius
            Aug 15 '17 at 9:37











          • Another one that you could mention is test instead of if or [. And if [ and if test seem to be interspersed with [ and test with no apparent rhyme or reason. test shows up on occasion, like at config.site for vendor libs on Fedora x86_64.

            – jww
            Oct 21 '17 at 4:45





















          48














          Here's my cheat sheet:




          • "A ; B" Run A and then B, regardless of success of A

          • "A && B" Run B if A succeeded

          • "A || B" Run B if A failed

          • "A &" Run A in background.






          share|improve this answer


























          • I like the cheat answers like yours. Thanks!

            – Tarik
            May 14 '17 at 2:34











          • There's some interesting lambda calculus parallels going on here demonstrated in working JavaScript(define the variables in order); "left(aka true)": (a => b => a). "right(aka false)": (a => b => b). "A; B" "then" (a => b => (() => b())(a())). "A && B" "if without else(when)" (a => b => a()(() => right)(b)). "A || B" "else without if(unless)" (a => b => a()(b)(() => right)). "a && b || c" "ifThenElse" (a => b => c => (unless(when(a)(b))(c))()).

            – Dmitry
            Jul 15 '17 at 4:53








          • 1





            note that in bash, left is analogous 0/empty string, right analogous is everything else.

            – Dmitry
            Jul 15 '17 at 5:00



















          22














          to expand on @Shawn-j-Goff's answer from above, && is a logical AND, and || is a logical OR.



          See this part of the Advanced Bash Scripting Guide. Some of the contents from the link for user reference as below.



          &&
          AND



          if [ $condition1 ] && [ $condition2 ]
          # Same as: if [ $condition1 -a $condition2 ]
          # Returns true if both condition1 and condition2 hold true...

          if [[ $condition1 && $condition2 ]] # Also works.
          # Note that && operator not permitted inside brackets
          #+ of [ ... ] construct.


          ||
          OR



          if [ $condition1 ] || [ $condition2 ]
          # Same as: if [ $condition1 -o $condition2 ]
          # Returns true if either condition1 or condition2 holds true...

          if [[ $condition1 || $condition2 ]] # Also works.
          # Note that || operator not permitted inside brackets
          #+ of a [ ... ] construct.





          share|improve this answer

































            9














            From my experience I use the && and || to reduce an if statement to a single line.



            Say we are looking for a file called /root/Sample.txt then the traditional iteration would be as follows in shell:



            if [ -f /root/Sample.txt ]
            then
            echo "file found"
            else
            echo "file not found"
            fi


            These 6 lines can be reduced to a single line:



            [[ -f /root/Sample.txt ]] && echo "file found" || echo "file not found"


            When running a few iterations to set variables or to create files etc., life is easier and the script looks slicker using the single line if function, it's only drawback is that it becomes a bit more difficult to implement multiple commands from a single iteration however you can make use of functions.






            share|improve this answer


























            • This is cool. Reminds me of objc code (a==b)?val=1:val=2;

              – GeneCode
              Aug 29 '17 at 4:13













            • How can i use this command when I want to run a process in the background with "&"? I get a syntax error for ./someScript.sh & && echo 'ok' || echo 'ko'

              – Katie S
              Mar 20 '18 at 22:54



















            3














            There is a notion of "short cutting".



            When (expr1 && expr2) is evaluated - expr2 is only evaluated if exp1 evaluates to "true". This is because both expr1 AND expr2 have to be true for (expr1 && expr2) to be true. If expr1 evaluates to "false" expr2 is NOT evalued (short cut) because (expr1 && expr2) is already "flase".



            Try the following - assume file F1 exists & file F2 does not exist:



            ( [ -s F1 ] && echo "File Exists" )  # will print "File Exists" - no short cut
            ( [ -s F2 ] && echo "File Exists" ) # will NOT print "File Exists" - short cut


            Similarly for || (or) - but short cutting is reversed.






            share|improve this answer





















            • 4





              The logic is usually referred to as "short circuiting". I've never seen the phrase "short cutting" used. I mention this to help with finding references.

              – L S
              Oct 28 '16 at 16:42



















            -1














            The examples in the answer by Shawn J. Goff are correct, but the explanation is the other way around. Please check:




            The right side of && will only be evaluated if the exit status of the left side is NONZERO. || is the opposite: it will evaluate the right side only if the left side exit status is ZERO.







            share|improve this answer





















            • 2





              You are aware that for shells zero exit code evaluates to true and thus nonzero exit code on the left side of && results in the whole expression to return false?

              – countermode
              Sep 29 '16 at 15:25













            • Someone did not approve your edits because it was not your answer. If you think that some answer is incorrect you should downvote it and possibly leave a comment; if you think that it requires a change, you should leave a comment. Editing is for correcting grammar, formatting, and spelling mistakes that do not change the meaning of the answer.

              – techraf
              Sep 29 '16 at 15:41













            • @countermode I'm sorry, you are right, I thought in Linux zero = false and nonzero = true (like in C and many other languages/environments/platforms). I apologize for the misunderstanding.

              – Try2Help
              Oct 5 '16 at 10:16










            protected by Kusalananda Sep 2 '17 at 9:10



            Thank you for your interest in this question.
            Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



            Would you like to answer one of these unanswered questions instead?














            6 Answers
            6






            active

            oldest

            votes








            6 Answers
            6






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            116














            The right side of && will only be evaluated if the exit status of the left side is zero (i.e. true). || is the opposite: it will evaluate the right side only if the left side exit status is non-zero (i.e. false).



            You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise.



            Examples:



            $ false && echo howdy!

            $ true && echo howdy!
            howdy!
            $ true || echo howdy!

            $ false || echo howdy!
            howdy!


            Extra notes:



            If you do which [, you might see that [ actually does point to a program! It's usually not actually the one that runs in scripts, though; run type [ to see what actually gets run. If you wan to try using the program, just give the full path like so: /bin/[ 1 = 1.






            share|improve this answer





















            • 6





              When you see "X or Y", you test X. If it's true, you already know the answer to "X or Y" (it's true), so no need to test Y. If it's true, you don't know the answer to "X or Y", so you do need to test Y. When you see "X and Y", you test X. If it's false, you already know the answer to "X and Y" (it's false), so no need to test Y. If X is true, then you do need to test Y to see if "X and Y" is true.

              – David Schwartz
              Nov 16 '11 at 3:41






            • 2





              Instead of "if the left side returns zero", I would write "if the left side command's exit status is zero". I find "return" a bit ambiguous as the output and the exit status can both be considered as "return values"

              – glenn jackman
              Nov 16 '11 at 14:36











            • Not only can you "consider [ ... ] to be a program", in many cases it is. It's commonly in the file /bin/[ or /usr/bin/[.

              – L S
              Oct 28 '16 at 16:39






            • 4





              C programmers will find this super confusing. There 0 means false... While in shellscripting 0 means true... Mind blown.

              – Calmarius
              Aug 15 '17 at 9:37











            • Another one that you could mention is test instead of if or [. And if [ and if test seem to be interspersed with [ and test with no apparent rhyme or reason. test shows up on occasion, like at config.site for vendor libs on Fedora x86_64.

              – jww
              Oct 21 '17 at 4:45


















            116














            The right side of && will only be evaluated if the exit status of the left side is zero (i.e. true). || is the opposite: it will evaluate the right side only if the left side exit status is non-zero (i.e. false).



            You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise.



            Examples:



            $ false && echo howdy!

            $ true && echo howdy!
            howdy!
            $ true || echo howdy!

            $ false || echo howdy!
            howdy!


            Extra notes:



            If you do which [, you might see that [ actually does point to a program! It's usually not actually the one that runs in scripts, though; run type [ to see what actually gets run. If you wan to try using the program, just give the full path like so: /bin/[ 1 = 1.






            share|improve this answer





















            • 6





              When you see "X or Y", you test X. If it's true, you already know the answer to "X or Y" (it's true), so no need to test Y. If it's true, you don't know the answer to "X or Y", so you do need to test Y. When you see "X and Y", you test X. If it's false, you already know the answer to "X and Y" (it's false), so no need to test Y. If X is true, then you do need to test Y to see if "X and Y" is true.

              – David Schwartz
              Nov 16 '11 at 3:41






            • 2





              Instead of "if the left side returns zero", I would write "if the left side command's exit status is zero". I find "return" a bit ambiguous as the output and the exit status can both be considered as "return values"

              – glenn jackman
              Nov 16 '11 at 14:36











            • Not only can you "consider [ ... ] to be a program", in many cases it is. It's commonly in the file /bin/[ or /usr/bin/[.

              – L S
              Oct 28 '16 at 16:39






            • 4





              C programmers will find this super confusing. There 0 means false... While in shellscripting 0 means true... Mind blown.

              – Calmarius
              Aug 15 '17 at 9:37











            • Another one that you could mention is test instead of if or [. And if [ and if test seem to be interspersed with [ and test with no apparent rhyme or reason. test shows up on occasion, like at config.site for vendor libs on Fedora x86_64.

              – jww
              Oct 21 '17 at 4:45
















            116












            116








            116







            The right side of && will only be evaluated if the exit status of the left side is zero (i.e. true). || is the opposite: it will evaluate the right side only if the left side exit status is non-zero (i.e. false).



            You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise.



            Examples:



            $ false && echo howdy!

            $ true && echo howdy!
            howdy!
            $ true || echo howdy!

            $ false || echo howdy!
            howdy!


            Extra notes:



            If you do which [, you might see that [ actually does point to a program! It's usually not actually the one that runs in scripts, though; run type [ to see what actually gets run. If you wan to try using the program, just give the full path like so: /bin/[ 1 = 1.






            share|improve this answer















            The right side of && will only be evaluated if the exit status of the left side is zero (i.e. true). || is the opposite: it will evaluate the right side only if the left side exit status is non-zero (i.e. false).



            You can consider [ ... ] to be a program with a return value. If the test inside evaluates to true, it returns zero; it returns nonzero otherwise.



            Examples:



            $ false && echo howdy!

            $ true && echo howdy!
            howdy!
            $ true || echo howdy!

            $ false || echo howdy!
            howdy!


            Extra notes:



            If you do which [, you might see that [ actually does point to a program! It's usually not actually the one that runs in scripts, though; run type [ to see what actually gets run. If you wan to try using the program, just give the full path like so: /bin/[ 1 = 1.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 1 hour ago









            Hashim

            1055




            1055










            answered Nov 16 '11 at 2:36









            Shawn J. GoffShawn J. Goff

            30k19112134




            30k19112134








            • 6





              When you see "X or Y", you test X. If it's true, you already know the answer to "X or Y" (it's true), so no need to test Y. If it's true, you don't know the answer to "X or Y", so you do need to test Y. When you see "X and Y", you test X. If it's false, you already know the answer to "X and Y" (it's false), so no need to test Y. If X is true, then you do need to test Y to see if "X and Y" is true.

              – David Schwartz
              Nov 16 '11 at 3:41






            • 2





              Instead of "if the left side returns zero", I would write "if the left side command's exit status is zero". I find "return" a bit ambiguous as the output and the exit status can both be considered as "return values"

              – glenn jackman
              Nov 16 '11 at 14:36











            • Not only can you "consider [ ... ] to be a program", in many cases it is. It's commonly in the file /bin/[ or /usr/bin/[.

              – L S
              Oct 28 '16 at 16:39






            • 4





              C programmers will find this super confusing. There 0 means false... While in shellscripting 0 means true... Mind blown.

              – Calmarius
              Aug 15 '17 at 9:37











            • Another one that you could mention is test instead of if or [. And if [ and if test seem to be interspersed with [ and test with no apparent rhyme or reason. test shows up on occasion, like at config.site for vendor libs on Fedora x86_64.

              – jww
              Oct 21 '17 at 4:45
















            • 6





              When you see "X or Y", you test X. If it's true, you already know the answer to "X or Y" (it's true), so no need to test Y. If it's true, you don't know the answer to "X or Y", so you do need to test Y. When you see "X and Y", you test X. If it's false, you already know the answer to "X and Y" (it's false), so no need to test Y. If X is true, then you do need to test Y to see if "X and Y" is true.

              – David Schwartz
              Nov 16 '11 at 3:41






            • 2





              Instead of "if the left side returns zero", I would write "if the left side command's exit status is zero". I find "return" a bit ambiguous as the output and the exit status can both be considered as "return values"

              – glenn jackman
              Nov 16 '11 at 14:36











            • Not only can you "consider [ ... ] to be a program", in many cases it is. It's commonly in the file /bin/[ or /usr/bin/[.

              – L S
              Oct 28 '16 at 16:39






            • 4





              C programmers will find this super confusing. There 0 means false... While in shellscripting 0 means true... Mind blown.

              – Calmarius
              Aug 15 '17 at 9:37











            • Another one that you could mention is test instead of if or [. And if [ and if test seem to be interspersed with [ and test with no apparent rhyme or reason. test shows up on occasion, like at config.site for vendor libs on Fedora x86_64.

              – jww
              Oct 21 '17 at 4:45










            6




            6





            When you see "X or Y", you test X. If it's true, you already know the answer to "X or Y" (it's true), so no need to test Y. If it's true, you don't know the answer to "X or Y", so you do need to test Y. When you see "X and Y", you test X. If it's false, you already know the answer to "X and Y" (it's false), so no need to test Y. If X is true, then you do need to test Y to see if "X and Y" is true.

            – David Schwartz
            Nov 16 '11 at 3:41





            When you see "X or Y", you test X. If it's true, you already know the answer to "X or Y" (it's true), so no need to test Y. If it's true, you don't know the answer to "X or Y", so you do need to test Y. When you see "X and Y", you test X. If it's false, you already know the answer to "X and Y" (it's false), so no need to test Y. If X is true, then you do need to test Y to see if "X and Y" is true.

            – David Schwartz
            Nov 16 '11 at 3:41




            2




            2





            Instead of "if the left side returns zero", I would write "if the left side command's exit status is zero". I find "return" a bit ambiguous as the output and the exit status can both be considered as "return values"

            – glenn jackman
            Nov 16 '11 at 14:36





            Instead of "if the left side returns zero", I would write "if the left side command's exit status is zero". I find "return" a bit ambiguous as the output and the exit status can both be considered as "return values"

            – glenn jackman
            Nov 16 '11 at 14:36













            Not only can you "consider [ ... ] to be a program", in many cases it is. It's commonly in the file /bin/[ or /usr/bin/[.

            – L S
            Oct 28 '16 at 16:39





            Not only can you "consider [ ... ] to be a program", in many cases it is. It's commonly in the file /bin/[ or /usr/bin/[.

            – L S
            Oct 28 '16 at 16:39




            4




            4





            C programmers will find this super confusing. There 0 means false... While in shellscripting 0 means true... Mind blown.

            – Calmarius
            Aug 15 '17 at 9:37





            C programmers will find this super confusing. There 0 means false... While in shellscripting 0 means true... Mind blown.

            – Calmarius
            Aug 15 '17 at 9:37













            Another one that you could mention is test instead of if or [. And if [ and if test seem to be interspersed with [ and test with no apparent rhyme or reason. test shows up on occasion, like at config.site for vendor libs on Fedora x86_64.

            – jww
            Oct 21 '17 at 4:45







            Another one that you could mention is test instead of if or [. And if [ and if test seem to be interspersed with [ and test with no apparent rhyme or reason. test shows up on occasion, like at config.site for vendor libs on Fedora x86_64.

            – jww
            Oct 21 '17 at 4:45















            48














            Here's my cheat sheet:




            • "A ; B" Run A and then B, regardless of success of A

            • "A && B" Run B if A succeeded

            • "A || B" Run B if A failed

            • "A &" Run A in background.






            share|improve this answer


























            • I like the cheat answers like yours. Thanks!

              – Tarik
              May 14 '17 at 2:34











            • There's some interesting lambda calculus parallels going on here demonstrated in working JavaScript(define the variables in order); "left(aka true)": (a => b => a). "right(aka false)": (a => b => b). "A; B" "then" (a => b => (() => b())(a())). "A && B" "if without else(when)" (a => b => a()(() => right)(b)). "A || B" "else without if(unless)" (a => b => a()(b)(() => right)). "a && b || c" "ifThenElse" (a => b => c => (unless(when(a)(b))(c))()).

              – Dmitry
              Jul 15 '17 at 4:53








            • 1





              note that in bash, left is analogous 0/empty string, right analogous is everything else.

              – Dmitry
              Jul 15 '17 at 5:00
















            48














            Here's my cheat sheet:




            • "A ; B" Run A and then B, regardless of success of A

            • "A && B" Run B if A succeeded

            • "A || B" Run B if A failed

            • "A &" Run A in background.






            share|improve this answer


























            • I like the cheat answers like yours. Thanks!

              – Tarik
              May 14 '17 at 2:34











            • There's some interesting lambda calculus parallels going on here demonstrated in working JavaScript(define the variables in order); "left(aka true)": (a => b => a). "right(aka false)": (a => b => b). "A; B" "then" (a => b => (() => b())(a())). "A && B" "if without else(when)" (a => b => a()(() => right)(b)). "A || B" "else without if(unless)" (a => b => a()(b)(() => right)). "a && b || c" "ifThenElse" (a => b => c => (unless(when(a)(b))(c))()).

              – Dmitry
              Jul 15 '17 at 4:53








            • 1





              note that in bash, left is analogous 0/empty string, right analogous is everything else.

              – Dmitry
              Jul 15 '17 at 5:00














            48












            48








            48







            Here's my cheat sheet:




            • "A ; B" Run A and then B, regardless of success of A

            • "A && B" Run B if A succeeded

            • "A || B" Run B if A failed

            • "A &" Run A in background.






            share|improve this answer















            Here's my cheat sheet:




            • "A ; B" Run A and then B, regardless of success of A

            • "A && B" Run B if A succeeded

            • "A || B" Run B if A failed

            • "A &" Run A in background.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Sep 29 '16 at 15:48









            Jeff Schaller

            43.3k1160140




            43.3k1160140










            answered Aug 18 '16 at 15:07









            user177073user177073

            636187




            636187













            • I like the cheat answers like yours. Thanks!

              – Tarik
              May 14 '17 at 2:34











            • There's some interesting lambda calculus parallels going on here demonstrated in working JavaScript(define the variables in order); "left(aka true)": (a => b => a). "right(aka false)": (a => b => b). "A; B" "then" (a => b => (() => b())(a())). "A && B" "if without else(when)" (a => b => a()(() => right)(b)). "A || B" "else without if(unless)" (a => b => a()(b)(() => right)). "a && b || c" "ifThenElse" (a => b => c => (unless(when(a)(b))(c))()).

              – Dmitry
              Jul 15 '17 at 4:53








            • 1





              note that in bash, left is analogous 0/empty string, right analogous is everything else.

              – Dmitry
              Jul 15 '17 at 5:00



















            • I like the cheat answers like yours. Thanks!

              – Tarik
              May 14 '17 at 2:34











            • There's some interesting lambda calculus parallels going on here demonstrated in working JavaScript(define the variables in order); "left(aka true)": (a => b => a). "right(aka false)": (a => b => b). "A; B" "then" (a => b => (() => b())(a())). "A && B" "if without else(when)" (a => b => a()(() => right)(b)). "A || B" "else without if(unless)" (a => b => a()(b)(() => right)). "a && b || c" "ifThenElse" (a => b => c => (unless(when(a)(b))(c))()).

              – Dmitry
              Jul 15 '17 at 4:53








            • 1





              note that in bash, left is analogous 0/empty string, right analogous is everything else.

              – Dmitry
              Jul 15 '17 at 5:00

















            I like the cheat answers like yours. Thanks!

            – Tarik
            May 14 '17 at 2:34





            I like the cheat answers like yours. Thanks!

            – Tarik
            May 14 '17 at 2:34













            There's some interesting lambda calculus parallels going on here demonstrated in working JavaScript(define the variables in order); "left(aka true)": (a => b => a). "right(aka false)": (a => b => b). "A; B" "then" (a => b => (() => b())(a())). "A && B" "if without else(when)" (a => b => a()(() => right)(b)). "A || B" "else without if(unless)" (a => b => a()(b)(() => right)). "a && b || c" "ifThenElse" (a => b => c => (unless(when(a)(b))(c))()).

            – Dmitry
            Jul 15 '17 at 4:53







            There's some interesting lambda calculus parallels going on here demonstrated in working JavaScript(define the variables in order); "left(aka true)": (a => b => a). "right(aka false)": (a => b => b). "A; B" "then" (a => b => (() => b())(a())). "A && B" "if without else(when)" (a => b => a()(() => right)(b)). "A || B" "else without if(unless)" (a => b => a()(b)(() => right)). "a && b || c" "ifThenElse" (a => b => c => (unless(when(a)(b))(c))()).

            – Dmitry
            Jul 15 '17 at 4:53






            1




            1





            note that in bash, left is analogous 0/empty string, right analogous is everything else.

            – Dmitry
            Jul 15 '17 at 5:00





            note that in bash, left is analogous 0/empty string, right analogous is everything else.

            – Dmitry
            Jul 15 '17 at 5:00











            22














            to expand on @Shawn-j-Goff's answer from above, && is a logical AND, and || is a logical OR.



            See this part of the Advanced Bash Scripting Guide. Some of the contents from the link for user reference as below.



            &&
            AND



            if [ $condition1 ] && [ $condition2 ]
            # Same as: if [ $condition1 -a $condition2 ]
            # Returns true if both condition1 and condition2 hold true...

            if [[ $condition1 && $condition2 ]] # Also works.
            # Note that && operator not permitted inside brackets
            #+ of [ ... ] construct.


            ||
            OR



            if [ $condition1 ] || [ $condition2 ]
            # Same as: if [ $condition1 -o $condition2 ]
            # Returns true if either condition1 or condition2 holds true...

            if [[ $condition1 || $condition2 ]] # Also works.
            # Note that || operator not permitted inside brackets
            #+ of a [ ... ] construct.





            share|improve this answer






























              22














              to expand on @Shawn-j-Goff's answer from above, && is a logical AND, and || is a logical OR.



              See this part of the Advanced Bash Scripting Guide. Some of the contents from the link for user reference as below.



              &&
              AND



              if [ $condition1 ] && [ $condition2 ]
              # Same as: if [ $condition1 -a $condition2 ]
              # Returns true if both condition1 and condition2 hold true...

              if [[ $condition1 && $condition2 ]] # Also works.
              # Note that && operator not permitted inside brackets
              #+ of [ ... ] construct.


              ||
              OR



              if [ $condition1 ] || [ $condition2 ]
              # Same as: if [ $condition1 -o $condition2 ]
              # Returns true if either condition1 or condition2 holds true...

              if [[ $condition1 || $condition2 ]] # Also works.
              # Note that || operator not permitted inside brackets
              #+ of a [ ... ] construct.





              share|improve this answer




























                22












                22








                22







                to expand on @Shawn-j-Goff's answer from above, && is a logical AND, and || is a logical OR.



                See this part of the Advanced Bash Scripting Guide. Some of the contents from the link for user reference as below.



                &&
                AND



                if [ $condition1 ] && [ $condition2 ]
                # Same as: if [ $condition1 -a $condition2 ]
                # Returns true if both condition1 and condition2 hold true...

                if [[ $condition1 && $condition2 ]] # Also works.
                # Note that && operator not permitted inside brackets
                #+ of [ ... ] construct.


                ||
                OR



                if [ $condition1 ] || [ $condition2 ]
                # Same as: if [ $condition1 -o $condition2 ]
                # Returns true if either condition1 or condition2 holds true...

                if [[ $condition1 || $condition2 ]] # Also works.
                # Note that || operator not permitted inside brackets
                #+ of a [ ... ] construct.





                share|improve this answer















                to expand on @Shawn-j-Goff's answer from above, && is a logical AND, and || is a logical OR.



                See this part of the Advanced Bash Scripting Guide. Some of the contents from the link for user reference as below.



                &&
                AND



                if [ $condition1 ] && [ $condition2 ]
                # Same as: if [ $condition1 -a $condition2 ]
                # Returns true if both condition1 and condition2 hold true...

                if [[ $condition1 && $condition2 ]] # Also works.
                # Note that && operator not permitted inside brackets
                #+ of [ ... ] construct.


                ||
                OR



                if [ $condition1 ] || [ $condition2 ]
                # Same as: if [ $condition1 -o $condition2 ]
                # Returns true if either condition1 or condition2 holds true...

                if [[ $condition1 || $condition2 ]] # Also works.
                # Note that || operator not permitted inside brackets
                #+ of a [ ... ] construct.






                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited Aug 29 '17 at 5:27









                GeneCode

                659




                659










                answered Nov 16 '11 at 6:08









                Tim KennedyTim Kennedy

                14.6k23051




                14.6k23051























                    9














                    From my experience I use the && and || to reduce an if statement to a single line.



                    Say we are looking for a file called /root/Sample.txt then the traditional iteration would be as follows in shell:



                    if [ -f /root/Sample.txt ]
                    then
                    echo "file found"
                    else
                    echo "file not found"
                    fi


                    These 6 lines can be reduced to a single line:



                    [[ -f /root/Sample.txt ]] && echo "file found" || echo "file not found"


                    When running a few iterations to set variables or to create files etc., life is easier and the script looks slicker using the single line if function, it's only drawback is that it becomes a bit more difficult to implement multiple commands from a single iteration however you can make use of functions.






                    share|improve this answer


























                    • This is cool. Reminds me of objc code (a==b)?val=1:val=2;

                      – GeneCode
                      Aug 29 '17 at 4:13













                    • How can i use this command when I want to run a process in the background with "&"? I get a syntax error for ./someScript.sh & && echo 'ok' || echo 'ko'

                      – Katie S
                      Mar 20 '18 at 22:54
















                    9














                    From my experience I use the && and || to reduce an if statement to a single line.



                    Say we are looking for a file called /root/Sample.txt then the traditional iteration would be as follows in shell:



                    if [ -f /root/Sample.txt ]
                    then
                    echo "file found"
                    else
                    echo "file not found"
                    fi


                    These 6 lines can be reduced to a single line:



                    [[ -f /root/Sample.txt ]] && echo "file found" || echo "file not found"


                    When running a few iterations to set variables or to create files etc., life is easier and the script looks slicker using the single line if function, it's only drawback is that it becomes a bit more difficult to implement multiple commands from a single iteration however you can make use of functions.






                    share|improve this answer


























                    • This is cool. Reminds me of objc code (a==b)?val=1:val=2;

                      – GeneCode
                      Aug 29 '17 at 4:13













                    • How can i use this command when I want to run a process in the background with "&"? I get a syntax error for ./someScript.sh & && echo 'ok' || echo 'ko'

                      – Katie S
                      Mar 20 '18 at 22:54














                    9












                    9








                    9







                    From my experience I use the && and || to reduce an if statement to a single line.



                    Say we are looking for a file called /root/Sample.txt then the traditional iteration would be as follows in shell:



                    if [ -f /root/Sample.txt ]
                    then
                    echo "file found"
                    else
                    echo "file not found"
                    fi


                    These 6 lines can be reduced to a single line:



                    [[ -f /root/Sample.txt ]] && echo "file found" || echo "file not found"


                    When running a few iterations to set variables or to create files etc., life is easier and the script looks slicker using the single line if function, it's only drawback is that it becomes a bit more difficult to implement multiple commands from a single iteration however you can make use of functions.






                    share|improve this answer















                    From my experience I use the && and || to reduce an if statement to a single line.



                    Say we are looking for a file called /root/Sample.txt then the traditional iteration would be as follows in shell:



                    if [ -f /root/Sample.txt ]
                    then
                    echo "file found"
                    else
                    echo "file not found"
                    fi


                    These 6 lines can be reduced to a single line:



                    [[ -f /root/Sample.txt ]] && echo "file found" || echo "file not found"


                    When running a few iterations to set variables or to create files etc., life is easier and the script looks slicker using the single line if function, it's only drawback is that it becomes a bit more difficult to implement multiple commands from a single iteration however you can make use of functions.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Nov 10 '17 at 8:48









                    Community

                    1




                    1










                    answered Jun 21 '16 at 0:15









                    syme89syme89

                    9111




                    9111













                    • This is cool. Reminds me of objc code (a==b)?val=1:val=2;

                      – GeneCode
                      Aug 29 '17 at 4:13













                    • How can i use this command when I want to run a process in the background with "&"? I get a syntax error for ./someScript.sh & && echo 'ok' || echo 'ko'

                      – Katie S
                      Mar 20 '18 at 22:54



















                    • This is cool. Reminds me of objc code (a==b)?val=1:val=2;

                      – GeneCode
                      Aug 29 '17 at 4:13













                    • How can i use this command when I want to run a process in the background with "&"? I get a syntax error for ./someScript.sh & && echo 'ok' || echo 'ko'

                      – Katie S
                      Mar 20 '18 at 22:54

















                    This is cool. Reminds me of objc code (a==b)?val=1:val=2;

                    – GeneCode
                    Aug 29 '17 at 4:13







                    This is cool. Reminds me of objc code (a==b)?val=1:val=2;

                    – GeneCode
                    Aug 29 '17 at 4:13















                    How can i use this command when I want to run a process in the background with "&"? I get a syntax error for ./someScript.sh & && echo 'ok' || echo 'ko'

                    – Katie S
                    Mar 20 '18 at 22:54





                    How can i use this command when I want to run a process in the background with "&"? I get a syntax error for ./someScript.sh & && echo 'ok' || echo 'ko'

                    – Katie S
                    Mar 20 '18 at 22:54











                    3














                    There is a notion of "short cutting".



                    When (expr1 && expr2) is evaluated - expr2 is only evaluated if exp1 evaluates to "true". This is because both expr1 AND expr2 have to be true for (expr1 && expr2) to be true. If expr1 evaluates to "false" expr2 is NOT evalued (short cut) because (expr1 && expr2) is already "flase".



                    Try the following - assume file F1 exists & file F2 does not exist:



                    ( [ -s F1 ] && echo "File Exists" )  # will print "File Exists" - no short cut
                    ( [ -s F2 ] && echo "File Exists" ) # will NOT print "File Exists" - short cut


                    Similarly for || (or) - but short cutting is reversed.






                    share|improve this answer





















                    • 4





                      The logic is usually referred to as "short circuiting". I've never seen the phrase "short cutting" used. I mention this to help with finding references.

                      – L S
                      Oct 28 '16 at 16:42
















                    3














                    There is a notion of "short cutting".



                    When (expr1 && expr2) is evaluated - expr2 is only evaluated if exp1 evaluates to "true". This is because both expr1 AND expr2 have to be true for (expr1 && expr2) to be true. If expr1 evaluates to "false" expr2 is NOT evalued (short cut) because (expr1 && expr2) is already "flase".



                    Try the following - assume file F1 exists & file F2 does not exist:



                    ( [ -s F1 ] && echo "File Exists" )  # will print "File Exists" - no short cut
                    ( [ -s F2 ] && echo "File Exists" ) # will NOT print "File Exists" - short cut


                    Similarly for || (or) - but short cutting is reversed.






                    share|improve this answer





















                    • 4





                      The logic is usually referred to as "short circuiting". I've never seen the phrase "short cutting" used. I mention this to help with finding references.

                      – L S
                      Oct 28 '16 at 16:42














                    3












                    3








                    3







                    There is a notion of "short cutting".



                    When (expr1 && expr2) is evaluated - expr2 is only evaluated if exp1 evaluates to "true". This is because both expr1 AND expr2 have to be true for (expr1 && expr2) to be true. If expr1 evaluates to "false" expr2 is NOT evalued (short cut) because (expr1 && expr2) is already "flase".



                    Try the following - assume file F1 exists & file F2 does not exist:



                    ( [ -s F1 ] && echo "File Exists" )  # will print "File Exists" - no short cut
                    ( [ -s F2 ] && echo "File Exists" ) # will NOT print "File Exists" - short cut


                    Similarly for || (or) - but short cutting is reversed.






                    share|improve this answer















                    There is a notion of "short cutting".



                    When (expr1 && expr2) is evaluated - expr2 is only evaluated if exp1 evaluates to "true". This is because both expr1 AND expr2 have to be true for (expr1 && expr2) to be true. If expr1 evaluates to "false" expr2 is NOT evalued (short cut) because (expr1 && expr2) is already "flase".



                    Try the following - assume file F1 exists & file F2 does not exist:



                    ( [ -s F1 ] && echo "File Exists" )  # will print "File Exists" - no short cut
                    ( [ -s F2 ] && echo "File Exists" ) # will NOT print "File Exists" - short cut


                    Similarly for || (or) - but short cutting is reversed.







                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Jan 8 '15 at 19:47









                    jasonwryan

                    50.4k14135189




                    50.4k14135189










                    answered Jan 8 '15 at 18:47









                    LarryLarry

                    311




                    311








                    • 4





                      The logic is usually referred to as "short circuiting". I've never seen the phrase "short cutting" used. I mention this to help with finding references.

                      – L S
                      Oct 28 '16 at 16:42














                    • 4





                      The logic is usually referred to as "short circuiting". I've never seen the phrase "short cutting" used. I mention this to help with finding references.

                      – L S
                      Oct 28 '16 at 16:42








                    4




                    4





                    The logic is usually referred to as "short circuiting". I've never seen the phrase "short cutting" used. I mention this to help with finding references.

                    – L S
                    Oct 28 '16 at 16:42





                    The logic is usually referred to as "short circuiting". I've never seen the phrase "short cutting" used. I mention this to help with finding references.

                    – L S
                    Oct 28 '16 at 16:42











                    -1














                    The examples in the answer by Shawn J. Goff are correct, but the explanation is the other way around. Please check:




                    The right side of && will only be evaluated if the exit status of the left side is NONZERO. || is the opposite: it will evaluate the right side only if the left side exit status is ZERO.







                    share|improve this answer





















                    • 2





                      You are aware that for shells zero exit code evaluates to true and thus nonzero exit code on the left side of && results in the whole expression to return false?

                      – countermode
                      Sep 29 '16 at 15:25













                    • Someone did not approve your edits because it was not your answer. If you think that some answer is incorrect you should downvote it and possibly leave a comment; if you think that it requires a change, you should leave a comment. Editing is for correcting grammar, formatting, and spelling mistakes that do not change the meaning of the answer.

                      – techraf
                      Sep 29 '16 at 15:41













                    • @countermode I'm sorry, you are right, I thought in Linux zero = false and nonzero = true (like in C and many other languages/environments/platforms). I apologize for the misunderstanding.

                      – Try2Help
                      Oct 5 '16 at 10:16
















                    -1














                    The examples in the answer by Shawn J. Goff are correct, but the explanation is the other way around. Please check:




                    The right side of && will only be evaluated if the exit status of the left side is NONZERO. || is the opposite: it will evaluate the right side only if the left side exit status is ZERO.







                    share|improve this answer





















                    • 2





                      You are aware that for shells zero exit code evaluates to true and thus nonzero exit code on the left side of && results in the whole expression to return false?

                      – countermode
                      Sep 29 '16 at 15:25













                    • Someone did not approve your edits because it was not your answer. If you think that some answer is incorrect you should downvote it and possibly leave a comment; if you think that it requires a change, you should leave a comment. Editing is for correcting grammar, formatting, and spelling mistakes that do not change the meaning of the answer.

                      – techraf
                      Sep 29 '16 at 15:41













                    • @countermode I'm sorry, you are right, I thought in Linux zero = false and nonzero = true (like in C and many other languages/environments/platforms). I apologize for the misunderstanding.

                      – Try2Help
                      Oct 5 '16 at 10:16














                    -1












                    -1








                    -1







                    The examples in the answer by Shawn J. Goff are correct, but the explanation is the other way around. Please check:




                    The right side of && will only be evaluated if the exit status of the left side is NONZERO. || is the opposite: it will evaluate the right side only if the left side exit status is ZERO.







                    share|improve this answer















                    The examples in the answer by Shawn J. Goff are correct, but the explanation is the other way around. Please check:




                    The right side of && will only be evaluated if the exit status of the left side is NONZERO. || is the opposite: it will evaluate the right side only if the left side exit status is ZERO.








                    share|improve this answer














                    share|improve this answer



                    share|improve this answer








                    edited Sep 29 '16 at 15:39









                    techraf

                    4,235102242




                    4,235102242










                    answered Sep 29 '16 at 15:14









                    Try2HelpTry2Help

                    1




                    1








                    • 2





                      You are aware that for shells zero exit code evaluates to true and thus nonzero exit code on the left side of && results in the whole expression to return false?

                      – countermode
                      Sep 29 '16 at 15:25













                    • Someone did not approve your edits because it was not your answer. If you think that some answer is incorrect you should downvote it and possibly leave a comment; if you think that it requires a change, you should leave a comment. Editing is for correcting grammar, formatting, and spelling mistakes that do not change the meaning of the answer.

                      – techraf
                      Sep 29 '16 at 15:41













                    • @countermode I'm sorry, you are right, I thought in Linux zero = false and nonzero = true (like in C and many other languages/environments/platforms). I apologize for the misunderstanding.

                      – Try2Help
                      Oct 5 '16 at 10:16














                    • 2





                      You are aware that for shells zero exit code evaluates to true and thus nonzero exit code on the left side of && results in the whole expression to return false?

                      – countermode
                      Sep 29 '16 at 15:25













                    • Someone did not approve your edits because it was not your answer. If you think that some answer is incorrect you should downvote it and possibly leave a comment; if you think that it requires a change, you should leave a comment. Editing is for correcting grammar, formatting, and spelling mistakes that do not change the meaning of the answer.

                      – techraf
                      Sep 29 '16 at 15:41













                    • @countermode I'm sorry, you are right, I thought in Linux zero = false and nonzero = true (like in C and many other languages/environments/platforms). I apologize for the misunderstanding.

                      – Try2Help
                      Oct 5 '16 at 10:16








                    2




                    2





                    You are aware that for shells zero exit code evaluates to true and thus nonzero exit code on the left side of && results in the whole expression to return false?

                    – countermode
                    Sep 29 '16 at 15:25







                    You are aware that for shells zero exit code evaluates to true and thus nonzero exit code on the left side of && results in the whole expression to return false?

                    – countermode
                    Sep 29 '16 at 15:25















                    Someone did not approve your edits because it was not your answer. If you think that some answer is incorrect you should downvote it and possibly leave a comment; if you think that it requires a change, you should leave a comment. Editing is for correcting grammar, formatting, and spelling mistakes that do not change the meaning of the answer.

                    – techraf
                    Sep 29 '16 at 15:41







                    Someone did not approve your edits because it was not your answer. If you think that some answer is incorrect you should downvote it and possibly leave a comment; if you think that it requires a change, you should leave a comment. Editing is for correcting grammar, formatting, and spelling mistakes that do not change the meaning of the answer.

                    – techraf
                    Sep 29 '16 at 15:41















                    @countermode I'm sorry, you are right, I thought in Linux zero = false and nonzero = true (like in C and many other languages/environments/platforms). I apologize for the misunderstanding.

                    – Try2Help
                    Oct 5 '16 at 10:16





                    @countermode I'm sorry, you are right, I thought in Linux zero = false and nonzero = true (like in C and many other languages/environments/platforms). I apologize for the misunderstanding.

                    – Try2Help
                    Oct 5 '16 at 10:16





                    protected by Kusalananda Sep 2 '17 at 9:10



                    Thank you for your interest in this question.
                    Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).



                    Would you like to answer one of these unanswered questions instead?



                    Popular posts from this blog

                    CARDNET

                    Boot-repair Failure: Unable to locate package grub-common:i386

                    濃尾地震