ksh: unset x same as x=""?

ksh: unset x same as x=""?

Post by wayne t. wats » Fri, 13 Jun 1997 04:00:00



That's the question.  Is unset x
the same as x="" when using the Korn shell?
--
    Wayne T. Watson  |                              |     /  *              
   The Software Hill |  Consulting in C, UNIX, C++  |    /              .    *
   Mountain View, CA |  Motif, X-Windows, Graphics  |   //            *
    (415) 969-4233   |    Statistics, Science,      |  o         *      .

        "Progress might have been all right once but it has gone on
        far too long."  Ogden Nash

 
 
 

ksh: unset x same as x=""?

Post by Brendan O'D » Fri, 13 Jun 1997 04:00:00




Quote:>That's the question.  Is unset x
>the same as x="" when using the Korn shell?

Depends what you are doing with $x.  When you use unset, you remove
the variable entirely, x= retains the variable but sets it to nothing.

As far as scripting is concerned, the constructs such as

    [[ -n $x ]] and [[ -z $x ]]

should work the same either way.  There is a difference for parameter
substitutions, where constructs such as:

    ${x-whatever}
    ${x:-whatever}

will substitute `whatever' in both cases for `unset x', but only in
the second (:) case for `x='.

Regards,
--

Compusol Pty. Limited                  (NSW, Australia)  +61 2 9809 0133

 
 
 

ksh: unset x same as x=""?

Post by Steffen Klu » Fri, 13 Jun 1997 04:00:00




Quote:>That's the question.  Is unset x
>the same as x="" when using the Korn shell?

No. `unset x' removes the shell variable x, whereas
`x=""' sets it to "" (the empty string). You can
easily verify this:

set -u (or set -o nounset)
x=""
echo $x
unset x
echo $x

Hope this helps
Steffen.

--

Fujitsu Australia Ltd
Keywords: photography, Mozart, UNIX, Islay Malt
--

 
 
 

ksh: unset x same as x=""?

Post by Michael We » Sat, 14 Jun 1997 04:00:00


Hallo wayne, zu "ksh: unset x same as x=""?" vom 12.06.97:

wtw>That's the question.  Is unset x
wtw>the same as x="" when using the Korn shell?

No. With "unset" you delete the variable. With x="" you define a variable,  
the value is a nullstring.

In korn shell it is the same thing in most cases, if you get the value of  
an unset variable, you will get no error. With the shell option

set -o nounset

you will get an error, if the variable is not defined.

Gruss, Michael