1

Just wondering if this:

if [ "$first_arg" == "major" ] || [ "$first_arg" == "minor" ]; then
        exit 1;
fi

is the same as this:

if [ "$first_arg" == "major" || "$first_arg" == "minor" ]; then
      exit 1;
fi
  • 2
    You don't need the double quotes with [[ ... ]]. – choroba Jun 16 '18 at 23:26
  • @choroba thanks, even if I quote one expression but not the other, you are saying that [[...]] will quote everything for me? – Alexander Mills Jun 16 '18 at 23:50
  • There's simply no word splitting nor pathname expansion in [[ ... ]]. See the end of my updated answer. – Tomasz Jun 17 '18 at 0:08
5

They're not the same. In fact [ "$first_arg" == "major" || "$first_arg" == "minor" ] is not even a valid expression.

This is because [ is a command that's equivalent to test and they can't use the || alternative, which operates on the inter-command level. What could be historically considered correct for alternative is -o, but it's now marked as obsolete by POSIX1, which advises to rewrite

test "$1" -o "$2"

into

test "$1" || test "$2"

Apart from the test and [ constructs, there's also the "modern" [[ test command, which in turn doesn't accept -o altogether, but instead accepts ||.

Thus all of these are valid and equivalent:

  1. One [[ test:

    if [[ $first_arg == major || $first_arg == minor ]]; then
        exit 1;
    fi
    
  2. Two [[ tests:

    if [[ $first_arg == major ]] || [[ $first_arg == minor ]]; then
        exit 1;
    fi
    
  3. Two [ tests (the standard equivalent):

    if [ "$first_arg" = major ] || [ "$first_arg" = minor ]; then
        exit 1;
    fi
    

Double quotes aroung $first_arg are not necessary inside [[, as there's no word splitting nor pathname expansion in there. The quotes should be used with [, however. And there's no point in quoting minor nor major either. Not just here, but with test or [ too. That's because they're simple strings.

1. See APPLICATION USAGE.

  • Also, == is not (generally) valid in [ ... ]. – Kusalananda Jun 17 '18 at 7:45
  • Also related: unix.stackexchange.com/questions/270847/… – Kusalananda Jun 17 '18 at 7:46
  • So in other words: both expressions are not valid -) – schily Jun 17 '18 at 11:37
  • Bash's documentation has == and = equivalent for string comparison. Though it states that = should be used with the test command for POSIX conformance. – Tomasz Jun 17 '18 at 11:52
  • 1
    Also: case $first_arg in (major|minor) exit 1; esac ;-) – Kusalananda Jun 17 '18 at 12:09
1

Since both of your expressions are not valid, there is a quasi equivalence ;-)

Note that == is only valid with the non-standard [[ oparator.

In general: when using the test builtin, it is recommended to use only simple expressions as larger expressions could result in unpredictable results in case that parameter expansion applies.

Since the test is built into the shell since approx. 35 years, calling more simple test commands and combine them at shell level is not a performance problem anymore.

  • == is accepted by Bash in [, but that's not POSIX compliant. – Tomasz Jun 17 '18 at 11:53
  • I know there's issues with -a/-o in [/test, but I do wonder what you refer to with macro expansion? – ilkkachu Jun 17 '18 at 12:10
  • macro expansion if the official wording for shell variable expansion. – schily Jun 17 '18 at 13:17
  • i guess they are semantically invalid – Alexander Mills Jun 17 '18 at 18:44
  • @schily, Can you tell me what texts use that phrase? All documentation for the shells I've looked at talk about parameter expansion or substitution, as do the Open Group specifications I've seen. – ilkkachu Jun 18 '18 at 9:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.