Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

unset, set variables, and arithmetic and other options

108 views
Skip to first unread message

Beta What

unread,
Feb 26, 2012, 1:46:55 AM2/26/12
to
Hi,

Does the shell (bash and csh) treat set variable that are set to empty
string "" same as unset variables. I believe csh requires variable to
be set. For bash, are unset and "" the same in all contexts? If not
can you point me to when they are not?

How about unset variable in operations, like arithmetic or tests?
From some tests it seems like bash treats unset variables as =0.

Ivan Shmakov

unread,
Feb 26, 2012, 2:55:04 AM2/26/12
to
>>>>> Beta What <lite.o...@gmail.com> writes:

[Cross-posting to news:comp.unix.shell, for obvious reasons.]

> Does the shell (bash and csh) treat set variable that are set to
> empty string "" same as unset variables. I believe csh requires
> variable to be set. For bash, are unset and "" the same in all
> contexts? If not can you point me to when they are not?

Consider, e. g.:

$ (a=value ; printf %s\\n "${a:-default}" "${a-default}")
value
value
$ (a= ; printf %s\\n "${a:-default}" "${a-default}")
default

$ (unset a ; printf %s\\n "${a:-default}" "${a-default}")
default
default
$

> How about unset variable in operations, like arithmetic or tests?
> From some tests it seems like bash treats unset variables as =0.

--
FSF associate member #7257

Beta What

unread,
Feb 26, 2012, 3:18:48 AM2/26/12
to
On Feb 26, 2:55 am, Ivan Shmakov <oneing...@gmail.com> wrote:
> >>>>> Beta What <lite.on.b...@gmail.com> writes:
>
>         [Cross-posting to news:comp.unix.shell, for obvious reasons.]
>
>  > Does the shell (bash and csh) treat set variable that are set to
>  > empty string "" same as unset variables.  I believe csh requires
>  > variable to be set.  For bash, are unset and "" the same in all
>  > contexts?  If not can you point me to when they are not?
>
>         Consider, e. g.:
>
> $ (a=value ; printf %s\\n "${a:-default}" "${a-default}")
> value
> value
> $ (a=      ; printf %s\\n "${a:-default}" "${a-default}")
> default
>
> $ (unset a ; printf %s\\n "${a:-default}" "${a-default}")
> default
> default
> $


Pre those special bash variable substitutions all other contexts of
unset and "" null strings were the same?

Janis Papanagnou

unread,
Feb 26, 2012, 4:59:38 AM2/26/12
to
What do you mean by "special bash variable substitutions"? - Those
substitutions are part of the POSIX shell standard, and have been
even in the bourne shell (and all descendent shells) since decades.

Unset and empty are not the same, as shown. There are contexts where
the difference don't matter, though; e.g. if you expand the variables
to obtain their value, which is in both cases just empty.

Janis

John McCue

unread,
Feb 26, 2012, 11:20:22 AM2/26/12
to
Beta What <lite.o...@gmail.com> wrote:
> Hi,
>
> Does the shell (bash and csh) treat set variable that are set to empty
> string "" same as unset variables. I believe csh requires variable to
> be set. For bash, are unset and "" the same in all contexts? If not
> can you point me to when they are not?

for tcsh (on many systems as csh) checking an unset
variable will result in this message:
z: Undefined variable.
where $z is the variable name. I believe this is the same
as the original csh, I can check tomorrow at work and will
reply if different.

You can check for a variable existance by:

if ($?z) then
...
endif

>
> How about unset variable in operations, like arithmetic or tests?
> From some tests it seems like bash treats unset variables as =0.
unset in tcsh will remove the variable and you will start
seeing the message above.

John

Geoff Clare

unread,
Feb 27, 2012, 8:14:47 AM2/27/12
to
Janis Papanagnou wrote:

> Unset and empty are not the same, as shown. There are contexts where
> the difference don't matter, though; e.g. if you expand the variables
> to obtain their value, which is in both cases just empty.

Unless "set -u" is in effect.

--
Geoff Clare <net...@gclare.org.uk>

Kaz Kylheku

unread,
Feb 27, 2012, 8:42:34 AM2/27/12
to
["Followup-To:" header set to comp.unix.shell.]
On 2012-02-27, Geoff Clare <ge...@clare.See-My-Signature.invalid> wrote:
> Janis Papanagnou wrote:
>
>> Unset and empty are not the same, as shown. There are contexts where
>> the difference don't matter, though; e.g. if you expand the variables
>> to obtain their value, which is in both cases just empty.
>
> Unless "set -u" is in effect.

+1

A darn good idea for any non-throwaway script longer than half a page.
0 new messages