[plug] How to check for an empty string in a bash script.

Bevan Broun bevanb at ee.uwa.edu.au
Wed Dec 15 11:00:42 WST 1999


on Wed, Dec 15, 1999 at 10:44:33AM +0800, Buddrige, David <david.buddrige at mitswa.com.au> wrote:
> Hi all, 
> 
> I have the following code in a bash script that I am writing...
> 
> if [ THE_PATH="" ]; then
> 	echo "The Path is:" $THE_PATH
> 	echo "FAIL! FILE DOES NOT EXIST."	
> fi
> 
> In another part of the script, the variable "THE_PATH" is populated with a
> filename that is the result of a search of the filesystem.  If the file was
> not found that value should be empty.  However, when I run this code, it is
> printing out the filename for which I searched (indicating that the variable
> "THE_PATH" is NOT empty, yet still it is making into the TRUE part of this
> if statement and subsequently echoing "FAIL! FILE DOES NOT EXIST."
> 
> My question:  How does one check a variable in a bash script to see if it is
> empty?

see http://207.213.123.70/book/ for an online bourne shell programming
reference. I think you are after -z 

if [ -z $THE_PATH ];then

BB
-- 
Bevan Broun                                           ph (08) 9380 1587
Computer Systems Officer                             fax (08) 9380 1065
Dept. Electrical and Electronic Engineering      
University of Western Australia                                 rm. G70


More information about the plug mailing list