Skip to main content
deleted 17 characters in body
Source Link
MC68020
  • 8.2k
  • 3
  • 19
  • 49

From man bash:

       -n string
              True if the length of string is non‐zero.

Examples:

# expected
$ var=""; [ -n "$var" ]; echo $?
1

# unexpected?
$ var=""; [ -n $var ]; echo $?
0

Here we see that -n with unquoted variable containing empty string returns true. Why?

Why is $var is required to be quoted here?

From man bash:

       -n string
              True if the length of string is non‐zero.

Examples:

# expected
$ var=""; [ -n "$var" ]; echo $?
1

# unexpected?
$ var=""; [ -n $var ]; echo $?
0

Here we see that -n with unquoted variable containing empty string returns true. Why?

Why $var is required to be quoted here?

From man bash:

-n string
    True if the length of string is non‐zero.

Examples:

# expected
$ var=""; [ -n "$var" ]; echo $?
1

# unexpected?
$ var=""; [ -n $var ]; echo $?
0

Here we see that -n with unquoted variable containing empty string returns true. Why?

Why is $var required to be quoted here?

4
ilkkachu
  • 141.4k
  • 16
  • 249
  • 418
Post Closed as "Duplicate" by muru bash
added 24 characters in body; edited title
Source Link
pmor
  • 619
  • 7
  • 13

Why does -n with unquoted variable containing empty string return true?

From man bash:

       -n string
              True if the length of string is non‐zero.

Examples:

# expected
$ var=""; [ -n "$var" ]; echo $?
1

# unexpected?
$ var=""; [ -n $var ]; echo $?
0

Here we see that -n with unquoted variable containing empty string returns true. Why?

Why $var is required to be quoted here?

Why does -n with unquoted variable return true?

From man bash:

       -n string
              True if the length of string is non‐zero.

Examples:

# expected
$ var=""; [ -n "$var" ]; echo $?
1

# unexpected?
$ var=""; [ -n $var ]; echo $?
0

Here we see that -n with unquoted variable returns true. Why?

Why $var is required to be quoted here?

Why does -n with unquoted variable containing empty string return true?

From man bash:

       -n string
              True if the length of string is non‐zero.

Examples:

# expected
$ var=""; [ -n "$var" ]; echo $?
1

# unexpected?
$ var=""; [ -n $var ]; echo $?
0

Here we see that -n with unquoted variable containing empty string returns true. Why?

Why $var is required to be quoted here?

edited tags
Link
MC68020
  • 8.2k
  • 3
  • 19
  • 49
Source Link
pmor
  • 619
  • 7
  • 13
Loading