Timeline for Is there any difference between [[ -n $1 ]] and [[ $1 ]] in bash?
Current License: CC BY-SA 4.0
5 events
| when toggle format | what | by | license | comment | |
|---|---|---|---|---|---|
| Nov 10 at 12:18 | comment | added | Austin Hemmelgarn | In addition to the excellent point raised by @ilkkachu, I would argue that even if [[ -n $x ]] and [[ $x ]] are semantically equivalent per the language spec, the first form is still preferred because it makes the intent more explicit. | |
| Nov 10 at 11:32 | comment | added | ilkkachu | Note that with the standard [ ... ] (single brackets), there is a difference between [ -n $x ] and [ $x ] -- the first doesn't actually even work. That's because the unquoted expansion makes the whole word $x disappear if the variable is unset or empty, and the resulting commands are effectively [ -n ] and [ ]. The first of those is truthy, the second falsy. Though the unquoted expansion brings other issues too, [[ ... ]] avoids them by being special syntax that doesn't apply word splitting within it. | |
| Nov 10 at 6:21 | history | became hot network question | |||
| Nov 9 at 22:27 | answer | added | choroba | timeline score: 15 | |
| Nov 9 at 22:09 | history | asked | CertainPensioner | CC BY-SA 4.0 |