Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

10
  • 9
    Also works with single brackets: [ -v name_of_var ]. Commented Jun 25, 2015 at 16:45
  • 11
    beware that for hashes and arrays, it returns false unless the variable has an element of key/indice "0". For namerefs, it tests whether the target is defined. It doesn't work for special parameters like $1, $-, $#... Commented Dec 1, 2015 at 17:33
  • 8
    This feature is only in the bash builtin test or [ ; It is not available in /usr/bin/test. Compare man test with help test. Commented Aug 26, 2016 at 17:46
  • @MarkLakata Right, because external commands cannot know the internal state of the shell. Commented Sep 1, 2016 at 15:29
  • 3
    Not sure why it's an accepted answer. OP did not limit shell to bash only. Commented Aug 16, 2019 at 8:26