Comparing two strings (one may be a null)



I have some variable foo, and I was expecting to be able to compare it
to a literal string "bar", so I coded:

if [ $foo = bar ] ; then
echo 'Ok, they are the same.'
fi

If foo is null, I get an error "unexpected operator". So I can't compare
a string if it has a null value.

I now try:

if [ -n $foo ] ; then
if [ $foo = bar ] ; then
echo 'Ok, they are the same.'
fi
fi

I am still getting the error, presumably because the first (outermost)
if conditional statement falls through because $foo is null.

What should I be doing here to compare $foo to the literal string "bar"?

Mark.

--
Mark Hobley,
393 Quinton Road West,
Quinton, BIRMINGHAM.
B32 1QE.
.



Relevant Pages

  • Re: Comparing two strings (one may be a null)
    ... echo 'Ok, they are the same.' ... I am still getting the error, presumably because the first if conditional statement falls through because $foo is null. ... What should I be doing here to compare $foo to the literal string "bar"? ...
    (comp.unix.shell)
  • Re: Comparing two strings (one may be a null)
    ... If foo is null, I get an error "unexpected operator". ...     echo 'Ok, they are the same.' ... What should I be doing here to compare $foo to the literal string "bar"? ...
    (comp.unix.shell)
  • Re: Comparing two strings (one may be a null)
    ... to a literal string "bar", ... echo 'Ok, they are the same.' ... If foo is null, I get an error "unexpected operator". ... What should I be doing here to compare $foo to the literal string "bar"? ...
    (comp.unix.shell)
  • Re: Comparing two strings (one may be a null)
    ... I have some variable foo, and I was expecting to be able to compare it ... If foo is null, I get an error "unexpected operator". ... What should I be doing here to compare $foo to the literal string "bar"? ...
    (comp.unix.shell)
  • Re: Session : Simple question
    ... > echo $Foo; ... > reloading of this page? ... compare $Foo to 0 ...
    (comp.lang.php)