1

I have this code that outputs:

1: '[' '[' "" = "" ']'
2: script.sh: line 7: [: too many arguments
3: '[' '[' artifact1,artifact2,artifact3,artifact4,artifact5 = "" ']'
4: '[' '[' 2.14 = "" ']'
5: '[' '[' N = "" ']'
6: script.sh: line 7: [: too many arguments

Here is the part of the code:

set -x
NEW_TAG = "";
TAGS = artifact1,artifact2,artifact3,artifact4,artifact5;
VERSION = 2.14;
FIX = N;

if [ [ "$NEW_TAG" = "" ] || [ "$TAGS" = "" ] || [ "$VERSION" = "" ] || [ "$FIX" = "" ] ];

I have already put double quotes in the variables but still I encounter this error. What could have I done wrong?

Thanks in advance. Cheers!

4
  • 2
    The [ is actually an alias for the test command. I recommend you read the manual page for test. Commented Oct 25, 2017 at 10:06
  • Yes, does this mean my syntax is wrong? I just compared $string1 = empty string or NULL. Commented Oct 25, 2017 at 10:11
  • 2
    Yes; [ is a command name, not a grouping operator. Commented Oct 25, 2017 at 12:40
  • 1
    Your variable assignments are also wrong; no spaces are allowed around the =: NEW_TAG="" (or just NEW_TAG=). Run your code through shellcheck.net to catch the more obvious errors. Commented Oct 25, 2017 at 12:55

1 Answer 1

2

[ is another name for the test command, not a grouping operator like parentheses.

Your if statement is equivalent to

if test [ "$NEW_TAG" = "" || ...

and test doesn't expect any more arguments if the first one is [ (in this case it has no special meaning; it's just a non-enpty string).

The correct way to use multiple tests is

if [ "$NEW_TAG" = "" ] || [ "$TAGS" = "" ] || [ "$VERSION" = "" ] || [ "$FIX" = "" ];

You don't need any grouping, although if you really wanted to, you can use { ... }:

if { [ "$NEW_TAG" = "" ] ||
     [ "$TAGS" = "" ] ||
     [ "$VERSION" = "" ] ||
     [ "$FIX" = "" ]; };

Note the ; used to terminate the command list inside the { ... }.

Also, it is a little shorter (with less room for error) to use the -z operator to check if a string is empty.

if [ -z "$NEW_TAG" ] || [ -z "$TAGS" ] || [ -z "$VERSION" ] || [ -z "$FIX" ]; then
Sign up to request clarification or add additional context in comments.

1 Comment

Wow thank you very much! Actually I'm debugging this script. Thank you thank you!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.