Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am looking for a test that would return true if the environment variable is defined and has a value different than 0, false or NULL.


if ... do ...

Note: under no circumstance the bash should complain about the variable not being defined or having a weird value, anything else than 0, empty, false, or not defined would be considered true.

share|improve this question
Not sure it can be done with less than 4 tests (according to… ). – user130370 Aug 29 '12 at 12:05

Such a test does not exist as it might in a high-level language such as Java or C#, but it is relatively easy to construct.

Three comments on your question as asked:

1: You do not work with literal "true"/"false" values in bash; all commands and conditional constructs yield an exit status, or logical result, which is either 0 (true) or non-0 (false).
Work with this, don't try to re-invent the wheel.

2: Try not to use upper-case parameter names unless they are environment variables; this is a simple convention that helps keep your code clean.

3: What, exactly, do you mean by "NULL"? The literal string NULL, an empty value, or something else?
The latter two concepts don't exist in bash; a NULL variable would be unset, which you're already checking for.

The most effective solution would be a function that accepts a variable (or parameter, as bash calls them), and checks for those conditions, then sets the exit status based on that, effectively condensing multiple truth values to a literal 0 or 1.

For example:

is_true() { if [[ "$1" = 0 ]]; then false; elif [[ -z "$1" ]]; then false; elif [[ "$1" =~ [Ff][Aa][Ll][Ss][Ee] ]]; then false; fi; }

  • is_true "1" yields 0, which is true
  • is_true "false" yields 1, which is false
  • a=10; is_true "$a" yields 0
  • grep -q something somefile yields 0 if something appears at least once in somefile.

NOTE that the latter example clearly shows that you don't NEED such a construct; everything that happens in bash already works with these basic 0/1 truth values.

But I'll grant that you might have to convert input from some gnarly external program :)

share|improve this answer
NULL as in empty. – sorin Aug 29 '12 at 13:02
In that case you may have to add an explicit check to see if it is unset, in addition to the other 3. – adaptr Aug 29 '12 at 13:48

I find these comparisons unuseful because they make appearance of code obscur and unobvious. I just use the nice feature of bash, that comes since v4.2, from the manpage:

   -v varname
          True if the shell variable varname is set (has been assigned a value).

Usage is very simple

if [ -v OUTPUT_TO_LOG ]; then
    exec > &>./logfile

In other words, to flag as enabled, just define. Try it!

$ :() { if [ -v VAR ]; then echo 'existed'; fi }
$ unset VAR && :          # no output
$ unset VAR && VAR= && :  # "existed"
$ unset VAR && VAR=1 && : # "existed"
share|improve this answer

You could do all three checks with regular expression matching (undefined parameters resolve to the empty string):

is_true() (
  shopt -s nocasematch
  [[ ! $1 =~ ^(false|0|)$ ]]
share|improve this answer
An empty parameter is not the same as an unset parameter - but for the purposes of equality, they behave the same. – adaptr Aug 29 '12 at 14:23
Modified the statement. – Thor Aug 29 '12 at 14:27
Function definition should be sub-shelled (parentheses instead of braces), otherwise shopt also changes the option in the callers environment. See my question about this here. – Thor Aug 29 '12 at 14:50
I did not know that one, interesting. – adaptr Aug 29 '12 at 14:54

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.