AkihikoTakahashi
AkihikoTakahashi

Reputation: 159

Why var=""; [ -n $var ] && echo T || echo F returns F?

I found the following command:

var=""; [ -n $var ] && echo T || echo F

This command returns F. In my understanding, $var is null so the command should return T.

I also checked this:

[ -n "" ] && echo T || echo F

This returns T as I expected.

I can not understand why

[ -n "" ] && echo T || echo F

returns T but

var="" [ -n $var ] && echo T || echo F

returns F?

I checked them on CentOS6.4 bash4.1.2

Upvotes: 2

Views: 163

Answers (2)

chepner
chepner

Reputation: 531708

Executive summary: always quote your parameter expansions unless you know why it should not be. Use [ -n "$var" ] instead.


When unquoted,

[ -n $var ]

expands to

[ -n ]

that is, the empty string is removed from the input and bash is left with a one-argument call to [, which means the command returns true if the argument is non-empty. -n here is not an operator; it is simply a non-empty string.

You need to quote the parameter expansion so that

[ -n "$var" ]

expands to

[ -n "" ]

which is a two-argument call to [, which will do as you expect: test if the second argument has non-zero length.

Upvotes: 2

devnull
devnull

Reputation: 123598

This difference in output in case when you use a variable is that you don't quote the variable.

When you say:

[ -n $var ] && echo T || echo F

the shell reads it as:

'[' -n ']'

and returns T. This case is equivalent to saying [ foo ] which would always be true.

On the other hand, when you say:

[ -n "" ] && echo T || echo F

the shell would read it as:

'[' -n '' ']'

and return F.

Upvotes: 2

Related Questions