Check if a variable has a value assigned? | Unix Linux Forums | Shell Programming and Scripting

  Go Back    

Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts and shell scripting languages here.

Check if a variable has a value assigned?

Shell Programming and Scripting

Closed Thread    
Thread Tools Search this Thread Display Modes
Old 05-03-2009
sumeet sumeet is offline
Registered User
Join Date: Nov 2006
Last Activity: 19 April 2012, 11:18 AM EDT
Posts: 63
Thanks: 3
Thanked 0 Times in 0 Posts
Check if a variable has a value assigned?


I want to check if a variable has a value assigned to it or not.

I can do following -

cat $Var > File1
if [ -s File1 ]
      echo "$Var has value"
      echo "$Var is null"

But I have to check for 3 Variables and I want to wrap it up in couple of unix statements.

Any suggestion is appreciated.

Sponsored Links
Old 05-03-2009
garskoci garskoci is offline
Registered User
Join Date: Nov 2008
Last Activity: 8 October 2014, 1:03 PM EDT
Location: Chicago, IL
Posts: 52
Thanks: 0
Thanked 4 Times in 4 Posts
Hi Sumeet,

It may be personal preference, but I would do something more like the following when checking if a variable has contents. Then you can easily check all three rather than writing files that will probably have to be clean up. Also, you can't always be sure that you can write a file. You may not have permissions or the filesystem could be full etc.

if [ "X"$Var == "X" ]
      echo "\$Var in NULL"
      echo "\$Var has a value of $Var"

Sponsored Links
Old 05-03-2009
danmero danmero is offline Forum Advisor  
Join Date: Nov 2007
Last Activity: 12 October 2014, 10:11 AM EDT
Location: H3X
Posts: 2,151
Thanks: 10
Thanked 118 Times in 111 Posts
man test

if [ -z $Var ]
      echo '$Var is null'
      echo '$Var has value'

Last edited by danmero; 05-03-2009 at 10:30 PM..
Old 05-04-2009
cfajohnson's Avatar
cfajohnson cfajohnson is offline Forum Advisor  
Shell programmer, author
Join Date: Mar 2007
Last Activity: 21 January 2015, 4:24 PM EST
Location: Toronto, Canada
Posts: 2,882
Thanks: 0
Thanked 116 Times in 105 Posts

if [ -z "${var+X}" ]
  echo var is unset
elif [ -z "$var" ]
  echo var is set but empty
  echo "The value of \$var is $var"

Sponsored Links
Closed Thread

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Check in awk about a value of variable tal Shell Programming and Scripting 3 03-30-2009 10:27 AM
How to check if a variable contains a . gekkos Shell Programming and Scripting 3 11-27-2008 03:00 PM
Return code of command assigned to variable browndr Shell Programming and Scripting 2 08-05-2008 09:26 AM
What does $? mean when assigned to a variable? ranjita.c UNIX for Dummies Questions & Answers 3 08-24-2006 10:45 AM
How to check parameter variable? negixx Shell Programming and Scripting 4 06-10-2005 06:37 PM

All times are GMT -4. The time now is 09:52 AM.