Why doesn't using multiple commands with a || or && conditional work?How can I test if a variable is empty or contains only spaces?Using sshpass, return code (exit status) differs for reasons unknown using valid commandsScript in C shell skipping over last else statement?Modify bash arguments if only one argument is setFunction that calls another function with list of arguments doesn't workC Shell Script syntax error “unexpected end of of file”“dash” arguments to shell scriptsHow to use as argument the characters + , - , x , / in a scriptCreate a menu driven script with do loop and case statement in bashA star is being printed for an empty directory after running a script to list the subfolderecho $HISTSIZE not printing when executed via shell script but works in command line

Why no variance term in Bayesian logistic regression?

Detention in 1997

Is it inappropriate for a student to attend their mentor's dissertation defense?

What does the expression "A Mann!" means

Is it logically or scientifically possible to artificially send energy to the body?

How much of data wrangling is a data scientist's job?

Why is it a bad idea to hire a hitman to eliminate most corrupt politicians?

How badly should I try to prevent a user from XSSing themselves?

Do scales need to be in alphabetical order?

How can I determine if the org that I'm currently connected to is a scratch org?

Can my sorcerer use a spellbook only to collect spells and scribe scrolls, not cast?

Assassin's bullet with mercury

How to show a landlord what we have in savings?

Watching something be piped to a file live with tail

What are some good books on Machine Learning and AI like Krugman, Wells and Graddy's "Essentials of Economics"

Why doesn't using multiple commands with a || or && conditional work?

I would say: "You are another teacher", but she is a woman and I am a man

What is a romance in Latin?

How seriously should I take size and weight limits of hand luggage?

Do UK voters know if their MP will be the Speaker of the House?

iPad being using in wall mount battery swollen

ssTTsSTtRrriinInnnnNNNIiinngg

Why do bosons tend to occupy the same state?

Venezuelan girlfriend wants to travel the USA to be with me. What is the process?



Why doesn't using multiple commands with a || or && conditional work?


How can I test if a variable is empty or contains only spaces?Using sshpass, return code (exit status) differs for reasons unknown using valid commandsScript in C shell skipping over last else statement?Modify bash arguments if only one argument is setFunction that calls another function with list of arguments doesn't workC Shell Script syntax error “unexpected end of of file”“dash” arguments to shell scriptsHow to use as argument the characters + , - , x , / in a scriptCreate a menu driven script with do loop and case statement in bashA star is being printed for an empty directory after running a script to list the subfolderecho $HISTSIZE not printing when executed via shell script but works in command line













9

















This works on a shell (bash, dash) prompt:



[ -z "" ] && echo A || echo B
A


However, I am trying to write a POSIX shell script, it starts like this:



#!/bin/sh

[ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1

readonly raw_input_string=$1

[ -z "$raw_input_string" ] && echo "The given argument is empty."; exit 1


And I don't know why, but I don't get the message:




The given argument is empty.




if I call the script like this:



./test_empty_argument ""


Why is that?










share|improve this question



















  • 3





    See How can I test if a variable is empty or contains only spaces? for ways on testing if a variable is empty, unset, or only contains blanks. The issue in this question has nothing to do with that.

    – ilkkachu
    10 hours ago












  • Just use if [ X”” = X”$var” ] ; then echo isempty ; fi

    – user2497
    10 hours ago






  • 2





    @user2497 There is no reason to use that in any shell released in the last 20 years. That's a workaround for old, buggy shells.

    – chepner
    9 hours ago












  • @chepner So it is not a valid solution? Something else must be used?

    – user2497
    9 hours ago






  • 3





    [ "" = "$var" ] would work fine; a quoted empty string will not be removed from the argument list of [. But that's not necessary either, because [ -z "$var" ] also works just fine.

    – chepner
    9 hours ago
















9

















This works on a shell (bash, dash) prompt:



[ -z "" ] && echo A || echo B
A


However, I am trying to write a POSIX shell script, it starts like this:



#!/bin/sh

[ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1

readonly raw_input_string=$1

[ -z "$raw_input_string" ] && echo "The given argument is empty."; exit 1


And I don't know why, but I don't get the message:




The given argument is empty.




if I call the script like this:



./test_empty_argument ""


Why is that?










share|improve this question



















  • 3





    See How can I test if a variable is empty or contains only spaces? for ways on testing if a variable is empty, unset, or only contains blanks. The issue in this question has nothing to do with that.

    – ilkkachu
    10 hours ago












  • Just use if [ X”” = X”$var” ] ; then echo isempty ; fi

    – user2497
    10 hours ago






  • 2





    @user2497 There is no reason to use that in any shell released in the last 20 years. That's a workaround for old, buggy shells.

    – chepner
    9 hours ago












  • @chepner So it is not a valid solution? Something else must be used?

    – user2497
    9 hours ago






  • 3





    [ "" = "$var" ] would work fine; a quoted empty string will not be removed from the argument list of [. But that's not necessary either, because [ -z "$var" ] also works just fine.

    – chepner
    9 hours ago














9












9








9










This works on a shell (bash, dash) prompt:



[ -z "" ] && echo A || echo B
A


However, I am trying to write a POSIX shell script, it starts like this:



#!/bin/sh

[ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1

readonly raw_input_string=$1

[ -z "$raw_input_string" ] && echo "The given argument is empty."; exit 1


And I don't know why, but I don't get the message:




The given argument is empty.




if I call the script like this:



./test_empty_argument ""


Why is that?










share|improve this question


















This works on a shell (bash, dash) prompt:



[ -z "" ] && echo A || echo B
A


However, I am trying to write a POSIX shell script, it starts like this:



#!/bin/sh

[ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1

readonly raw_input_string=$1

[ -z "$raw_input_string" ] && echo "The given argument is empty."; exit 1


And I don't know why, but I don't get the message:




The given argument is empty.




if I call the script like this:



./test_empty_argument ""


Why is that?







shell-script arguments






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 7 hours ago









ilkkachu

63k10103180




63k10103180










asked 21 hours ago









VlastimilVlastimil

8,4451565145




8,4451565145







  • 3





    See How can I test if a variable is empty or contains only spaces? for ways on testing if a variable is empty, unset, or only contains blanks. The issue in this question has nothing to do with that.

    – ilkkachu
    10 hours ago












  • Just use if [ X”” = X”$var” ] ; then echo isempty ; fi

    – user2497
    10 hours ago






  • 2





    @user2497 There is no reason to use that in any shell released in the last 20 years. That's a workaround for old, buggy shells.

    – chepner
    9 hours ago












  • @chepner So it is not a valid solution? Something else must be used?

    – user2497
    9 hours ago






  • 3





    [ "" = "$var" ] would work fine; a quoted empty string will not be removed from the argument list of [. But that's not necessary either, because [ -z "$var" ] also works just fine.

    – chepner
    9 hours ago













  • 3





    See How can I test if a variable is empty or contains only spaces? for ways on testing if a variable is empty, unset, or only contains blanks. The issue in this question has nothing to do with that.

    – ilkkachu
    10 hours ago












  • Just use if [ X”” = X”$var” ] ; then echo isempty ; fi

    – user2497
    10 hours ago






  • 2





    @user2497 There is no reason to use that in any shell released in the last 20 years. That's a workaround for old, buggy shells.

    – chepner
    9 hours ago












  • @chepner So it is not a valid solution? Something else must be used?

    – user2497
    9 hours ago






  • 3





    [ "" = "$var" ] would work fine; a quoted empty string will not be removed from the argument list of [. But that's not necessary either, because [ -z "$var" ] also works just fine.

    – chepner
    9 hours ago








3




3





See How can I test if a variable is empty or contains only spaces? for ways on testing if a variable is empty, unset, or only contains blanks. The issue in this question has nothing to do with that.

– ilkkachu
10 hours ago






See How can I test if a variable is empty or contains only spaces? for ways on testing if a variable is empty, unset, or only contains blanks. The issue in this question has nothing to do with that.

– ilkkachu
10 hours ago














Just use if [ X”” = X”$var” ] ; then echo isempty ; fi

– user2497
10 hours ago





Just use if [ X”” = X”$var” ] ; then echo isempty ; fi

– user2497
10 hours ago




2




2





@user2497 There is no reason to use that in any shell released in the last 20 years. That's a workaround for old, buggy shells.

– chepner
9 hours ago






@user2497 There is no reason to use that in any shell released in the last 20 years. That's a workaround for old, buggy shells.

– chepner
9 hours ago














@chepner So it is not a valid solution? Something else must be used?

– user2497
9 hours ago





@chepner So it is not a valid solution? Something else must be used?

– user2497
9 hours ago




3




3





[ "" = "$var" ] would work fine; a quoted empty string will not be removed from the argument list of [. But that's not necessary either, because [ -z "$var" ] also works just fine.

– chepner
9 hours ago






[ "" = "$var" ] would work fine; a quoted empty string will not be removed from the argument list of [. But that's not necessary either, because [ -z "$var" ] also works just fine.

– chepner
9 hours ago











5 Answers
5






active

oldest

votes


















26














Note that your line



[ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


this is the same as



[ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."
exit 1


(a ; can, in most circumstances, be replaced by a newline character)



Which means that the exit 1 statement is always executed regardless of how many arguments were passed to the script. This means that the message The given argument is empty. would never have a chance of getting printed.



To execute more than a single statement after a test using the "short-circuit syntax", group the statements in ...; . The alternative is to use a proper if test (which, IMHO, looks cleaner in a script):



if [ "$#" -ne 1 ]; then
echo 'Invalid number of arguments, expected one.' >&2
exit 1
fi


You have the same issue with your second test.




Regarding



[ -z "" ] && echo A || echo B


This would work for the given example, but the generic



some-test && command1 || command2


would not be the same as



if some-test; then
command1
else
command2
fi


Instead, it is more like



if ! some-test && command1; ; then
command2
fi


That is, if either the test or the first command fails, the second command executes, which means it has the potential to execute all three involved statements.






share|improve this answer
































    11














    This:



    [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


    is not:



    [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1; 


    But instead is:



     echo "Invalid number of arguments, expected one."; 
    exit 1


    Your script is exiting regardless of how many arguments you passed to it.






    share|improve this answer






























      2














      One way to make it more readable is to define a die function (à la perl) like:



      die() 
      printf >&2 '%sn' "$@"
      exit 1


      # then:

      [ "$#" -eq 1 ] || die "Expected one argument, got $#"

      [ -n "$1" ] || die "Empty argument not supported"


      You can add more bells and whistles like colours, prefix... if need be.






      share|improve this answer























      • In practice, do you ever call your die function with multiple arguments? (If so, can you give an example?) I use an almost identical die function, but use "$*" instead, which may be more what you're intending?

        – jrw32982
        9 hours ago











      • The value of "$@" is that it allows multi-line messages without needing to add literal newlines.

        – Charles Duffy
        6 hours ago


















      0














      I've often seen this as a test for an empty string:



      if [ "x$foo" = "x" ]; then ...





      share|improve this answer

























      • Should have been "=" - fixed.

        – wef
        20 hours ago






      • 2





        That practice is literally from the 1970s. There is no reason whatsoever to use it with any shell that is compliant with the 1992 POSIX sh standard (so long as correct quoting is used and now-obsolescent functionality such as -a, -o, ( and ) as derectives to tell test to combine multiple operations in a single invocation are avoided; see the OB markers in pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html).

        – Charles Duffy
        7 hours ago












      • Non-linux unices shipped with the original 'sh' well into the '90's - maybe still do. In my job at that time, I had to write portable install scripts and I used this construct. I just looked at the install scripts that NVidia ships for linux and they still use this construct.

        – wef
        5 hours ago











      • NVidia may use it, but that's not to say they have any technical justification to do so; cargo-cult development in commercial UNIX is sadly prevalent. Even Heirloom Bourne doesn't have the bug in question -- so the SunOS codebase (that being the last commercial UNIX to ship a non-POSIX /bin/sh, and Heirloom Bourne's immediate predecessor) didn't have it either.

        – Charles Duffy
        4 hours ago







      • 1





        I don't wear a hat, so I can't promise to post a YouTube video eating it should someone turn up a shell published on a commercial UNIX with this bug post-1990... but if I did, it would be tempting. :)

        – Charles Duffy
        4 hours ago


















      0














      I would also add to the other answers that a POSIX shell may detect variable assignment errors.



      #!/bin/sh -u

      foo=$1:? # choose a meaninful variable name and possibly use an error message.
      # ... if the first parameter is unset or empty the shell exits.
      bar=$2- # if the second parameter is set, then the shell assigns its value, else
      # it assigns the empty string.

      # displays the date if "var" is empty, else, prints the working directory.
      [ -z "$var-" ] && date || pwd

      # "var" is set to the empty string. I assume the nineth parameter is not set.
      # `echo "$9"' returns 1 so the shell also runs `echo "var not empty: $var"'
      [ -z "$var-" ] && echo "$9" || echo "var not empty: $var"

      # `echo "var empty"' returns 0
      [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"

      var=0101

      [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"


      The shell exits if any variable is unset while the shell option -u is enabled. The shell expansions used in the sample script are also defined by POSIX.






      share|improve this answer

























        Your Answer








        StackExchange.ready(function()
        var channelOptions =
        tags: "".split(" "),
        id: "106"
        ;
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function()
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled)
        StackExchange.using("snippets", function()
        createEditor();
        );

        else
        createEditor();

        );

        function createEditor()
        StackExchange.prepareEditor(
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        bindNavPrevention: true,
        postfix: "",
        imageUploader:
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        ,
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        );



        );













        draft saved

        draft discarded


















        StackExchange.ready(
        function ()
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f510216%2fwhy-doesnt-using-multiple-commands-with-a-or-conditional-work%23new-answer', 'question_page');

        );

        Post as a guest















        Required, but never shown

























        5 Answers
        5






        active

        oldest

        votes








        5 Answers
        5






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        26














        Note that your line



        [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


        this is the same as



        [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."
        exit 1


        (a ; can, in most circumstances, be replaced by a newline character)



        Which means that the exit 1 statement is always executed regardless of how many arguments were passed to the script. This means that the message The given argument is empty. would never have a chance of getting printed.



        To execute more than a single statement after a test using the "short-circuit syntax", group the statements in ...; . The alternative is to use a proper if test (which, IMHO, looks cleaner in a script):



        if [ "$#" -ne 1 ]; then
        echo 'Invalid number of arguments, expected one.' >&2
        exit 1
        fi


        You have the same issue with your second test.




        Regarding



        [ -z "" ] && echo A || echo B


        This would work for the given example, but the generic



        some-test && command1 || command2


        would not be the same as



        if some-test; then
        command1
        else
        command2
        fi


        Instead, it is more like



        if ! some-test && command1; ; then
        command2
        fi


        That is, if either the test or the first command fails, the second command executes, which means it has the potential to execute all three involved statements.






        share|improve this answer





























          26














          Note that your line



          [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


          this is the same as



          [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."
          exit 1


          (a ; can, in most circumstances, be replaced by a newline character)



          Which means that the exit 1 statement is always executed regardless of how many arguments were passed to the script. This means that the message The given argument is empty. would never have a chance of getting printed.



          To execute more than a single statement after a test using the "short-circuit syntax", group the statements in ...; . The alternative is to use a proper if test (which, IMHO, looks cleaner in a script):



          if [ "$#" -ne 1 ]; then
          echo 'Invalid number of arguments, expected one.' >&2
          exit 1
          fi


          You have the same issue with your second test.




          Regarding



          [ -z "" ] && echo A || echo B


          This would work for the given example, but the generic



          some-test && command1 || command2


          would not be the same as



          if some-test; then
          command1
          else
          command2
          fi


          Instead, it is more like



          if ! some-test && command1; ; then
          command2
          fi


          That is, if either the test or the first command fails, the second command executes, which means it has the potential to execute all three involved statements.






          share|improve this answer



























            26












            26








            26







            Note that your line



            [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


            this is the same as



            [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."
            exit 1


            (a ; can, in most circumstances, be replaced by a newline character)



            Which means that the exit 1 statement is always executed regardless of how many arguments were passed to the script. This means that the message The given argument is empty. would never have a chance of getting printed.



            To execute more than a single statement after a test using the "short-circuit syntax", group the statements in ...; . The alternative is to use a proper if test (which, IMHO, looks cleaner in a script):



            if [ "$#" -ne 1 ]; then
            echo 'Invalid number of arguments, expected one.' >&2
            exit 1
            fi


            You have the same issue with your second test.




            Regarding



            [ -z "" ] && echo A || echo B


            This would work for the given example, but the generic



            some-test && command1 || command2


            would not be the same as



            if some-test; then
            command1
            else
            command2
            fi


            Instead, it is more like



            if ! some-test && command1; ; then
            command2
            fi


            That is, if either the test or the first command fails, the second command executes, which means it has the potential to execute all three involved statements.






            share|improve this answer















            Note that your line



            [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


            this is the same as



            [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."
            exit 1


            (a ; can, in most circumstances, be replaced by a newline character)



            Which means that the exit 1 statement is always executed regardless of how many arguments were passed to the script. This means that the message The given argument is empty. would never have a chance of getting printed.



            To execute more than a single statement after a test using the "short-circuit syntax", group the statements in ...; . The alternative is to use a proper if test (which, IMHO, looks cleaner in a script):



            if [ "$#" -ne 1 ]; then
            echo 'Invalid number of arguments, expected one.' >&2
            exit 1
            fi


            You have the same issue with your second test.




            Regarding



            [ -z "" ] && echo A || echo B


            This would work for the given example, but the generic



            some-test && command1 || command2


            would not be the same as



            if some-test; then
            command1
            else
            command2
            fi


            Instead, it is more like



            if ! some-test && command1; ; then
            command2
            fi


            That is, if either the test or the first command fails, the second command executes, which means it has the potential to execute all three involved statements.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited 6 hours ago

























            answered 21 hours ago









            KusalanandaKusalananda

            139k17260432




            139k17260432























                11














                This:



                [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


                is not:



                [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1; 


                But instead is:



                 echo "Invalid number of arguments, expected one."; 
                exit 1


                Your script is exiting regardless of how many arguments you passed to it.






                share|improve this answer



























                  11














                  This:



                  [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


                  is not:



                  [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1; 


                  But instead is:



                   echo "Invalid number of arguments, expected one."; 
                  exit 1


                  Your script is exiting regardless of how many arguments you passed to it.






                  share|improve this answer

























                    11












                    11








                    11







                    This:



                    [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


                    is not:



                    [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1; 


                    But instead is:



                     echo "Invalid number of arguments, expected one."; 
                    exit 1


                    Your script is exiting regardless of how many arguments you passed to it.






                    share|improve this answer













                    This:



                    [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1


                    is not:



                    [ "$#" -eq 1 ] || echo "Invalid number of arguments, expected one."; exit 1; 


                    But instead is:



                     echo "Invalid number of arguments, expected one."; 
                    exit 1


                    Your script is exiting regardless of how many arguments you passed to it.







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered 21 hours ago









                    murumuru

                    37k589164




                    37k589164





















                        2














                        One way to make it more readable is to define a die function (à la perl) like:



                        die() 
                        printf >&2 '%sn' "$@"
                        exit 1


                        # then:

                        [ "$#" -eq 1 ] || die "Expected one argument, got $#"

                        [ -n "$1" ] || die "Empty argument not supported"


                        You can add more bells and whistles like colours, prefix... if need be.






                        share|improve this answer























                        • In practice, do you ever call your die function with multiple arguments? (If so, can you give an example?) I use an almost identical die function, but use "$*" instead, which may be more what you're intending?

                          – jrw32982
                          9 hours ago











                        • The value of "$@" is that it allows multi-line messages without needing to add literal newlines.

                          – Charles Duffy
                          6 hours ago















                        2














                        One way to make it more readable is to define a die function (à la perl) like:



                        die() 
                        printf >&2 '%sn' "$@"
                        exit 1


                        # then:

                        [ "$#" -eq 1 ] || die "Expected one argument, got $#"

                        [ -n "$1" ] || die "Empty argument not supported"


                        You can add more bells and whistles like colours, prefix... if need be.






                        share|improve this answer























                        • In practice, do you ever call your die function with multiple arguments? (If so, can you give an example?) I use an almost identical die function, but use "$*" instead, which may be more what you're intending?

                          – jrw32982
                          9 hours ago











                        • The value of "$@" is that it allows multi-line messages without needing to add literal newlines.

                          – Charles Duffy
                          6 hours ago













                        2












                        2








                        2







                        One way to make it more readable is to define a die function (à la perl) like:



                        die() 
                        printf >&2 '%sn' "$@"
                        exit 1


                        # then:

                        [ "$#" -eq 1 ] || die "Expected one argument, got $#"

                        [ -n "$1" ] || die "Empty argument not supported"


                        You can add more bells and whistles like colours, prefix... if need be.






                        share|improve this answer













                        One way to make it more readable is to define a die function (à la perl) like:



                        die() 
                        printf >&2 '%sn' "$@"
                        exit 1


                        # then:

                        [ "$#" -eq 1 ] || die "Expected one argument, got $#"

                        [ -n "$1" ] || die "Empty argument not supported"


                        You can add more bells and whistles like colours, prefix... if need be.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered 10 hours ago









                        Stéphane ChazelasStéphane Chazelas

                        313k57592948




                        313k57592948












                        • In practice, do you ever call your die function with multiple arguments? (If so, can you give an example?) I use an almost identical die function, but use "$*" instead, which may be more what you're intending?

                          – jrw32982
                          9 hours ago











                        • The value of "$@" is that it allows multi-line messages without needing to add literal newlines.

                          – Charles Duffy
                          6 hours ago

















                        • In practice, do you ever call your die function with multiple arguments? (If so, can you give an example?) I use an almost identical die function, but use "$*" instead, which may be more what you're intending?

                          – jrw32982
                          9 hours ago











                        • The value of "$@" is that it allows multi-line messages without needing to add literal newlines.

                          – Charles Duffy
                          6 hours ago
















                        In practice, do you ever call your die function with multiple arguments? (If so, can you give an example?) I use an almost identical die function, but use "$*" instead, which may be more what you're intending?

                        – jrw32982
                        9 hours ago





                        In practice, do you ever call your die function with multiple arguments? (If so, can you give an example?) I use an almost identical die function, but use "$*" instead, which may be more what you're intending?

                        – jrw32982
                        9 hours ago













                        The value of "$@" is that it allows multi-line messages without needing to add literal newlines.

                        – Charles Duffy
                        6 hours ago





                        The value of "$@" is that it allows multi-line messages without needing to add literal newlines.

                        – Charles Duffy
                        6 hours ago











                        0














                        I've often seen this as a test for an empty string:



                        if [ "x$foo" = "x" ]; then ...





                        share|improve this answer

























                        • Should have been "=" - fixed.

                          – wef
                          20 hours ago






                        • 2





                          That practice is literally from the 1970s. There is no reason whatsoever to use it with any shell that is compliant with the 1992 POSIX sh standard (so long as correct quoting is used and now-obsolescent functionality such as -a, -o, ( and ) as derectives to tell test to combine multiple operations in a single invocation are avoided; see the OB markers in pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html).

                          – Charles Duffy
                          7 hours ago












                        • Non-linux unices shipped with the original 'sh' well into the '90's - maybe still do. In my job at that time, I had to write portable install scripts and I used this construct. I just looked at the install scripts that NVidia ships for linux and they still use this construct.

                          – wef
                          5 hours ago











                        • NVidia may use it, but that's not to say they have any technical justification to do so; cargo-cult development in commercial UNIX is sadly prevalent. Even Heirloom Bourne doesn't have the bug in question -- so the SunOS codebase (that being the last commercial UNIX to ship a non-POSIX /bin/sh, and Heirloom Bourne's immediate predecessor) didn't have it either.

                          – Charles Duffy
                          4 hours ago







                        • 1





                          I don't wear a hat, so I can't promise to post a YouTube video eating it should someone turn up a shell published on a commercial UNIX with this bug post-1990... but if I did, it would be tempting. :)

                          – Charles Duffy
                          4 hours ago















                        0














                        I've often seen this as a test for an empty string:



                        if [ "x$foo" = "x" ]; then ...





                        share|improve this answer

























                        • Should have been "=" - fixed.

                          – wef
                          20 hours ago






                        • 2





                          That practice is literally from the 1970s. There is no reason whatsoever to use it with any shell that is compliant with the 1992 POSIX sh standard (so long as correct quoting is used and now-obsolescent functionality such as -a, -o, ( and ) as derectives to tell test to combine multiple operations in a single invocation are avoided; see the OB markers in pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html).

                          – Charles Duffy
                          7 hours ago












                        • Non-linux unices shipped with the original 'sh' well into the '90's - maybe still do. In my job at that time, I had to write portable install scripts and I used this construct. I just looked at the install scripts that NVidia ships for linux and they still use this construct.

                          – wef
                          5 hours ago











                        • NVidia may use it, but that's not to say they have any technical justification to do so; cargo-cult development in commercial UNIX is sadly prevalent. Even Heirloom Bourne doesn't have the bug in question -- so the SunOS codebase (that being the last commercial UNIX to ship a non-POSIX /bin/sh, and Heirloom Bourne's immediate predecessor) didn't have it either.

                          – Charles Duffy
                          4 hours ago







                        • 1





                          I don't wear a hat, so I can't promise to post a YouTube video eating it should someone turn up a shell published on a commercial UNIX with this bug post-1990... but if I did, it would be tempting. :)

                          – Charles Duffy
                          4 hours ago













                        0












                        0








                        0







                        I've often seen this as a test for an empty string:



                        if [ "x$foo" = "x" ]; then ...





                        share|improve this answer















                        I've often seen this as a test for an empty string:



                        if [ "x$foo" = "x" ]; then ...






                        share|improve this answer














                        share|improve this answer



                        share|improve this answer








                        edited 20 hours ago

























                        answered 20 hours ago









                        wefwef

                        31415




                        31415












                        • Should have been "=" - fixed.

                          – wef
                          20 hours ago






                        • 2





                          That practice is literally from the 1970s. There is no reason whatsoever to use it with any shell that is compliant with the 1992 POSIX sh standard (so long as correct quoting is used and now-obsolescent functionality such as -a, -o, ( and ) as derectives to tell test to combine multiple operations in a single invocation are avoided; see the OB markers in pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html).

                          – Charles Duffy
                          7 hours ago












                        • Non-linux unices shipped with the original 'sh' well into the '90's - maybe still do. In my job at that time, I had to write portable install scripts and I used this construct. I just looked at the install scripts that NVidia ships for linux and they still use this construct.

                          – wef
                          5 hours ago











                        • NVidia may use it, but that's not to say they have any technical justification to do so; cargo-cult development in commercial UNIX is sadly prevalent. Even Heirloom Bourne doesn't have the bug in question -- so the SunOS codebase (that being the last commercial UNIX to ship a non-POSIX /bin/sh, and Heirloom Bourne's immediate predecessor) didn't have it either.

                          – Charles Duffy
                          4 hours ago







                        • 1





                          I don't wear a hat, so I can't promise to post a YouTube video eating it should someone turn up a shell published on a commercial UNIX with this bug post-1990... but if I did, it would be tempting. :)

                          – Charles Duffy
                          4 hours ago

















                        • Should have been "=" - fixed.

                          – wef
                          20 hours ago






                        • 2





                          That practice is literally from the 1970s. There is no reason whatsoever to use it with any shell that is compliant with the 1992 POSIX sh standard (so long as correct quoting is used and now-obsolescent functionality such as -a, -o, ( and ) as derectives to tell test to combine multiple operations in a single invocation are avoided; see the OB markers in pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html).

                          – Charles Duffy
                          7 hours ago












                        • Non-linux unices shipped with the original 'sh' well into the '90's - maybe still do. In my job at that time, I had to write portable install scripts and I used this construct. I just looked at the install scripts that NVidia ships for linux and they still use this construct.

                          – wef
                          5 hours ago











                        • NVidia may use it, but that's not to say they have any technical justification to do so; cargo-cult development in commercial UNIX is sadly prevalent. Even Heirloom Bourne doesn't have the bug in question -- so the SunOS codebase (that being the last commercial UNIX to ship a non-POSIX /bin/sh, and Heirloom Bourne's immediate predecessor) didn't have it either.

                          – Charles Duffy
                          4 hours ago







                        • 1





                          I don't wear a hat, so I can't promise to post a YouTube video eating it should someone turn up a shell published on a commercial UNIX with this bug post-1990... but if I did, it would be tempting. :)

                          – Charles Duffy
                          4 hours ago
















                        Should have been "=" - fixed.

                        – wef
                        20 hours ago





                        Should have been "=" - fixed.

                        – wef
                        20 hours ago




                        2




                        2





                        That practice is literally from the 1970s. There is no reason whatsoever to use it with any shell that is compliant with the 1992 POSIX sh standard (so long as correct quoting is used and now-obsolescent functionality such as -a, -o, ( and ) as derectives to tell test to combine multiple operations in a single invocation are avoided; see the OB markers in pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html).

                        – Charles Duffy
                        7 hours ago






                        That practice is literally from the 1970s. There is no reason whatsoever to use it with any shell that is compliant with the 1992 POSIX sh standard (so long as correct quoting is used and now-obsolescent functionality such as -a, -o, ( and ) as derectives to tell test to combine multiple operations in a single invocation are avoided; see the OB markers in pubs.opengroup.org/onlinepubs/9699919799/utilities/test.html).

                        – Charles Duffy
                        7 hours ago














                        Non-linux unices shipped with the original 'sh' well into the '90's - maybe still do. In my job at that time, I had to write portable install scripts and I used this construct. I just looked at the install scripts that NVidia ships for linux and they still use this construct.

                        – wef
                        5 hours ago





                        Non-linux unices shipped with the original 'sh' well into the '90's - maybe still do. In my job at that time, I had to write portable install scripts and I used this construct. I just looked at the install scripts that NVidia ships for linux and they still use this construct.

                        – wef
                        5 hours ago













                        NVidia may use it, but that's not to say they have any technical justification to do so; cargo-cult development in commercial UNIX is sadly prevalent. Even Heirloom Bourne doesn't have the bug in question -- so the SunOS codebase (that being the last commercial UNIX to ship a non-POSIX /bin/sh, and Heirloom Bourne's immediate predecessor) didn't have it either.

                        – Charles Duffy
                        4 hours ago






                        NVidia may use it, but that's not to say they have any technical justification to do so; cargo-cult development in commercial UNIX is sadly prevalent. Even Heirloom Bourne doesn't have the bug in question -- so the SunOS codebase (that being the last commercial UNIX to ship a non-POSIX /bin/sh, and Heirloom Bourne's immediate predecessor) didn't have it either.

                        – Charles Duffy
                        4 hours ago





                        1




                        1





                        I don't wear a hat, so I can't promise to post a YouTube video eating it should someone turn up a shell published on a commercial UNIX with this bug post-1990... but if I did, it would be tempting. :)

                        – Charles Duffy
                        4 hours ago





                        I don't wear a hat, so I can't promise to post a YouTube video eating it should someone turn up a shell published on a commercial UNIX with this bug post-1990... but if I did, it would be tempting. :)

                        – Charles Duffy
                        4 hours ago











                        0














                        I would also add to the other answers that a POSIX shell may detect variable assignment errors.



                        #!/bin/sh -u

                        foo=$1:? # choose a meaninful variable name and possibly use an error message.
                        # ... if the first parameter is unset or empty the shell exits.
                        bar=$2- # if the second parameter is set, then the shell assigns its value, else
                        # it assigns the empty string.

                        # displays the date if "var" is empty, else, prints the working directory.
                        [ -z "$var-" ] && date || pwd

                        # "var" is set to the empty string. I assume the nineth parameter is not set.
                        # `echo "$9"' returns 1 so the shell also runs `echo "var not empty: $var"'
                        [ -z "$var-" ] && echo "$9" || echo "var not empty: $var"

                        # `echo "var empty"' returns 0
                        [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"

                        var=0101

                        [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"


                        The shell exits if any variable is unset while the shell option -u is enabled. The shell expansions used in the sample script are also defined by POSIX.






                        share|improve this answer





























                          0














                          I would also add to the other answers that a POSIX shell may detect variable assignment errors.



                          #!/bin/sh -u

                          foo=$1:? # choose a meaninful variable name and possibly use an error message.
                          # ... if the first parameter is unset or empty the shell exits.
                          bar=$2- # if the second parameter is set, then the shell assigns its value, else
                          # it assigns the empty string.

                          # displays the date if "var" is empty, else, prints the working directory.
                          [ -z "$var-" ] && date || pwd

                          # "var" is set to the empty string. I assume the nineth parameter is not set.
                          # `echo "$9"' returns 1 so the shell also runs `echo "var not empty: $var"'
                          [ -z "$var-" ] && echo "$9" || echo "var not empty: $var"

                          # `echo "var empty"' returns 0
                          [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"

                          var=0101

                          [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"


                          The shell exits if any variable is unset while the shell option -u is enabled. The shell expansions used in the sample script are also defined by POSIX.






                          share|improve this answer



























                            0












                            0








                            0







                            I would also add to the other answers that a POSIX shell may detect variable assignment errors.



                            #!/bin/sh -u

                            foo=$1:? # choose a meaninful variable name and possibly use an error message.
                            # ... if the first parameter is unset or empty the shell exits.
                            bar=$2- # if the second parameter is set, then the shell assigns its value, else
                            # it assigns the empty string.

                            # displays the date if "var" is empty, else, prints the working directory.
                            [ -z "$var-" ] && date || pwd

                            # "var" is set to the empty string. I assume the nineth parameter is not set.
                            # `echo "$9"' returns 1 so the shell also runs `echo "var not empty: $var"'
                            [ -z "$var-" ] && echo "$9" || echo "var not empty: $var"

                            # `echo "var empty"' returns 0
                            [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"

                            var=0101

                            [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"


                            The shell exits if any variable is unset while the shell option -u is enabled. The shell expansions used in the sample script are also defined by POSIX.






                            share|improve this answer















                            I would also add to the other answers that a POSIX shell may detect variable assignment errors.



                            #!/bin/sh -u

                            foo=$1:? # choose a meaninful variable name and possibly use an error message.
                            # ... if the first parameter is unset or empty the shell exits.
                            bar=$2- # if the second parameter is set, then the shell assigns its value, else
                            # it assigns the empty string.

                            # displays the date if "var" is empty, else, prints the working directory.
                            [ -z "$var-" ] && date || pwd

                            # "var" is set to the empty string. I assume the nineth parameter is not set.
                            # `echo "$9"' returns 1 so the shell also runs `echo "var not empty: $var"'
                            [ -z "$var-" ] && echo "$9" || echo "var not empty: $var"

                            # `echo "var empty"' returns 0
                            [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"

                            var=0101

                            [ -z "$var-" ] && echo "var empty" || echo "var not empty: $var"


                            The shell exits if any variable is unset while the shell option -u is enabled. The shell expansions used in the sample script are also defined by POSIX.







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited 10 hours ago

























                            answered 11 hours ago









                            FólkvangrFólkvangr

                            32913




                            32913



























                                draft saved

                                draft discarded
















































                                Thanks for contributing an answer to Unix & Linux Stack Exchange!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid


                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.

                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function ()
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2funix.stackexchange.com%2fquestions%2f510216%2fwhy-doesnt-using-multiple-commands-with-a-or-conditional-work%23new-answer', 'question_page');

                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                How to create a command for the “strange m” symbol in latex? Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 23, 2019 at 23:30 UTC (7:30pm US/Eastern)How do you make your own symbol when Detexify fails?Writing bold small caps with mathpazo packageplus-minus symbol with parenthesis around the minus signGreek character in Beamer document titleHow to create dashed right arrow over symbol?Currency symbol: Turkish LiraDouble prec as a single symbol?Plus Sign Too Big; How to Call adfbullet?Is there a TeX macro for three-legged pi?How do I get my integral-like symbol to align like the integral?How to selectively substitute a letter with another symbol representing the same letterHow do I generate a less than symbol and vertical bar that are the same height?

                                Българска екзархия Съдържание История | Български екзарси | Вижте също | Външни препратки | Литература | Бележки | НавигацияУстав за управлението на българската екзархия. Цариград, 1870Слово на Ловешкия митрополит Иларион при откриването на Българския народен събор в Цариград на 23. II. 1870 г.Българската правда и гръцката кривда. От С. М. (= Софийски Мелетий). Цариград, 1872Предстоятели на Българската екзархияПодмененият ВеликденИнформационна агенция „Фокус“Димитър Ризов. Българите в техните исторически, етнографически и политически граници (Атлас съдържащ 40 карти). Berlin, Königliche Hoflithographie, Hof-Buch- und -Steindruckerei Wilhelm Greve, 1917Report of the International Commission to Inquire into the Causes and Conduct of the Balkan Wars

                                Чепеларе Съдържание География | История | Население | Спортни и природни забележителности | Културни и исторически обекти | Религии | Обществени институции | Известни личности | Редовни събития | Галерия | Източници | Литература | Външни препратки | Навигация41°43′23.99″ с. ш. 24°41′09.99″ и. д. / 41.723333° с. ш. 24.686111° и. д.*ЧепелареЧепеларски Linux fest 2002Начало на Зимен сезон 2005/06Национални хайдушки празници „Капитан Петко Войвода“Град ЧепелареЧепеларе – народният ски курортbgrod.orgwww.terranatura.hit.bgСправка за населението на гр. Исперих, общ. Исперих, обл. РазградМузей на родопския карстМузей на спорта и скитеЧепеларебългарскибългарскианглийскитукИстория на градаСки писти в ЧепелареВремето в ЧепелареРадио и телевизия в ЧепелареЧепеларе мами с родопски чар и добри пистиЕвтин туризъм и снежни атракции в ЧепелареМестоположениеИнформация и снимки от музея на родопския карст3D панорами от ЧепелареЧепелареррр