1

I am setting an environment variable in my terminal with no value.

setenv ABC

Now I want to check in my bash script if this variable is set or not.

#!/bin/bash        

ABC_USAGE=0

if [[ -z "$ABC" ]] ;  then
  ABC_USAGE=$((ABC_USAGE+1))
fi
 echo $ABC_USAGE

I want to increase $ABC_USUAGE value only if $ABC is set in terminal without any value. My code is increasing this value anyway. which is not expected result. please help..

anubhava
  • 761,203
  • 64
  • 569
  • 643
XYZ_Linux
  • 3,247
  • 5
  • 31
  • 34

1 Answers1

4

use this instead of the if expression that you are using

if [ -z ${ABC+x} ]

this will evaluate to be nothing if the variable isn't set and if the variable will be set the condition will be true.

This expression:

if [[ -z "$ABC" ]]

only checks if the variable has any value assigned or if the variable is empty

it will always be true if you just set the variable and the variable has no value assigned to it.

Inder
  • 3,711
  • 9
  • 27
  • 42