3

Any idea why echo statement is being executed in both scenarios?

Outputs it worked:

#!/bin/bash

rizwan=''
if [ -n $rizwan ]; then
  echo "it worked"
fi

Replace -n with -z, and it outputs it worked when expecting no output:

#!/bin/bash

rizwan=''
if [ -z $rizwan ]; then
  echo "it worked"
fi
1
  • 8
    shellcheck.net tells you what is wrong with your script. Get into to the habit of using it.
    – DavidPostill
    Mar 18, 2020 at 10:59

2 Answers 2

8

You may know it or not, but let's make it clear: [ is not a part of the if syntax. I mean if [ … behaves like if true, if false or if any_command, where [, true, false and any_command are commands. They return exit status and this is what matters to if.

Even if [ is a builtin (and it is in Bash), it behaves like a regular command. There's even a standalone [ executable (e.g. /usr/bin/[) because it's required by POSIX.

This means [ -n whatever ] is just a [ command with few arguments. Yes, ] is just an argument, it's not a delimiter or anything like that. The command named [ just requires ] to be its last argument. This way the code is more legible, ] serves no other purpose.

The command test is equivalent to [. The only difference is test does not require ].
[ -n whatever ] is equivalent to test -n whatever.

If the shell expands $rizwan to an empty string, the snippets [ -n $rizwan ] and [ -z $rizwan ] become [ -n ] and [ -z ] respectively. The [ command is not aware there was something between -n (or -z) and ] in your code, it gets exactly two arguments. Both tests are treated as the [ STRING ] syntax where STRING is either -n or -z. This syntax tests if STRING is not empty. Your tests always succeed because neither -n nor -z is an empty string.

To make the snippets work as [ -n STRING ] or [ -z STRING ], you need to double-quote the variable. The shell, after expanding [ -z "$rizwan" ], will pass exactly three arguments to [. They will be -z, the expanded value of the variable (even if it's an empty string) and ]. The tool will get all the arguments you wanted it to get.

If unquoted $rizwan in your examples expanded to more than one word, [ would get more than three arguments. In general the final result would depend on the variable content and the implementation of [.

Compare Why does my shell script choke on whitespace or other special characters?.

Note [[ in Bash is different. It's a keyword, it's integrated with the shell parser, it changes some rules, it's actually aware of variables and quotes between [[ and ]]. If you used [[ instead of [ (and consequently ]] instead of ]), then your code would work as you expected. See this answer.

[ is the portable one.

4

As variable rizwan is empty, your if command becomes:

if [ -n  ]

The syntax [ STRING ] tests if the string is empty (no characters). So in your case of [ -n ], the string -n is evidently not-empty.

2
  • As per my understanding if [ -n $variable ] means to check if length of the variable is greater than zero . if yes than execute THEN section of script otherwise do not execute THEN section . how to overcome this.
    – rizwan
    Mar 18, 2020 at 12:33
  • 1
    [ is a synonym for the test command but requires a final argument of ]. This command has a lot of options and [ STRING ] is only one of them. See the test man page.
    – harrymc
    Mar 18, 2020 at 13:58

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .