1819809 Members
2834 Online
109607 Solutions
New Discussion юеВ

Test if variable is set

 
SOLVED
Go to solution
Wim Korten
Advisor

Test if variable is set

Hi all,

What is the best way to test if a variable has been set? (ksh)

Like:
if (variable set?)
do something
fi

Thx
Limited by Technology
16 REPLIES 16
Nicolas Dumeige
Esteemed Contributor

Re: Test if variable is set

Hello,

You could test if the variable has any lenght with test :

[ -z "$var" ] && echo "length of string var is zero"

Cheers

Nicolas
All different, all Unix
Zeev Schultz
Honored Contributor

Re: Test if variable is set

Doing 'echo $parameter' and 'echo $?'

like here:

[ring]//usr/include/sys:echo $PWD
/usr/include/sys
[ring]//usr/include/sys:echo $?
0
[ring]//usr/include/sys:echo $rest
sh: rest: Parameter not set.
[ring]//usr/include/sys:echo $?
1
[ring]//usr/include/sys:

exit status 1 returns if command exit status wasn't ok (ie parameter doesn't exist).

So computers don't think yet. At least not chess computers. - Seymour Cray
Jason Morgan_4
New Member

Re: Test if variable is set

Try

echo ${variable}

or

if [ ${variable) = ""
then
echo "null"
else
{do something)
fi
Jean-Luc Oudart
Honored Contributor

Re: Test if variable is set

You can check the varaible length this is close. try this script :
#!/bin/ksh

A1="abcd"
unset A2
A3=""

if [[ ! -z $A1 ]]; then
echo "A1 is set"
else
echo "A1 is not set"
fi

if [[ ! -z $A2 ]]; then
echo "A2 is set"
else
echo "A2 is not set"
fi

if [[ ! -z $A3 ]]; then
echo "A3 is set"
else
echo "A3 is not set"
fi

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

A1 is set
A2 is not set
A3 is not set

In fact A3 is set (and empty).

Regards,
Jean-Luc
fiat lux
KapilRaj
Honored Contributor

Re: Test if variable is set

if [ $VAR ]
then
execute
else
echo .....
fi

Kaps
Nothing is impossible
Wim Korten
Advisor

Re: Test if variable is set

If I understand all your replies correctly, there is no command to check on the existance op a variable, like you can do test -f for existance of a file.

In my case I want to check if the LANG variable is set. Is there a difference in behavior between:
1) no LANG variable set
2) LANG variable set to nothing (empty string)

THX
Limited by Technology
G. Vrijhoeven
Honored Contributor

Re: Test if variable is set

Hi Wim,

env command will list vars. so
TEST=`env | grep -q LANG`
if [ $? -ne 0 ]
then
echo "LANG is not set"
else
echo "LANG is $LANG"
fi

HTH,

Gideon
Wim Korten
Advisor

Re: Test if variable is set

Hi Gideon,

I was looking at env also, but what if env returns:
...
X=LANGUAGE
NLS_LANG=DUTCH_THE NETHERLANDS.WE8ISO8859P15
LANG=Dutch
...
How can if find only the LANG variable?
Limited by Technology
G. Vrijhoeven
Honored Contributor

Re: Test if variable is set

Hi,

env | grep ^LANG the ^states the line must start with LANG


HTH,

Gideon
Nicolas Dumeige
Esteemed Contributor

Re: Test if variable is set

Wim,

The shell deal with memory allocation on its own. Therefore, the declaration of a shell variable or the test of its existence can just take the form a value lookup. If you ask the value of a non-existent variable, the shell will tel you it's value is NULL (nothing) but it won't give you any error.

Cheers

Nicolas

All different, all Unix
Bill Hassell
Honored Contributor
Solution

Re: Test if variable is set

This is a common question, especially if you use set -u (abort if an undefined variabnle is accessed). Here's how I do it:

set -u

# Set any variables that might not be defined with a default
# value. That way, we can leave set -u and still check unset values

UNSET=IamNOTset
DT=${DT:-$UNSET}
VUE=${VUE:-$UNSET}

# now check if the variable was ever set:

if [ "$VUE" = $UNSET -o "$DT" = $UNSET ]
then
... setup for non-Xwindow stuff
else
... setup for Xwindows
fi

# In case this is a 'sourced' script,
# return the unset variables backup to
# their original state:

[ $DT = $UNSET ] && unset DT
[ $VUE = $UNSET ] && unset VUE

---
The key to handling an unset variable is to use the shell contstruct ${VAR_NAME:-something} which ensures that the variable is defined with a unique value (IamNOTset) so it can be tested later.


Bill Hassell, sysadmin
Jean-Luc Oudart
Honored Contributor

Re: Test if variable is set

Othwerwise you can force the value if not set (or nul value) :
${A1:="zxy"}
will initialise A1 if not set (or is null) with "zxy"

Regards,
Jean-Luc
fiat lux
Mark Grant
Honored Contributor

Re: Test if variable is set

One nasty little construct I've seen is as follows.

[ "A$VAR" = "A" ] && echo "$VAR is not set"
Never preceed any demonstration with anything more predictive than "watch this"
Jean-Luc Oudart
Honored Contributor

Re: Test if variable is set

Mark,

I think if VAR is set but empty this would return "not set"

Regards,
Jean-Luc
fiat lux
Mark Grant
Honored Contributor

Re: Test if variable is set

Thanks for that one Jean-Luc, you're right. I obviously got a bit too excited at suddenly and unexpectedly finding some time to ITRC :)
Never preceed any demonstration with anything more predictive than "watch this"
Wim Korten
Advisor

Re: Test if variable is set

I'll go for:

if ( env|grep -q ^LANG= ); then
unset LANG
fi

THX All for your replies!
Limited by Technology