Environment variables with empty value missing from ENV?

Under Windows XP (using cygwin’s bash, but native Ruby), I
found the following six-legged feature:

$ ruby -v
ruby 1.8.2 (2004-12-25) [i386-mswin32]
$ unset FOO; env|grep FOO; ruby -e ‘p ENV[“FOO”]’
nil
$ FOO= env |grep FOO
FOO=
$ FOO= ruby -e ‘p ENV[“FOO”]’
nil
$ FOO=xyz ruby -e ‘p ENV[“FOO”]’
xyz
$

The problem is that ENV doesn’t contain the key FOO in
the 2nd case, where the value is zero-length.

What possible justification could there be for this behaviour?

Clifford H…

Clifford H. wrote:

Under Windows XP (using cygwin’s bash, but native Ruby), I
found the following six-legged feature:

$ ruby -v
ruby 1.8.2 (2004-12-25) [i386-mswin32]
Have you tried this with Ruby 1.8.4? What does “which ruby” say?

$ unset FOO; env|grep FOO; ruby -e ‘p ENV[“FOO”]’
nil
$ FOO= env |grep FOO
FOO=
Is this legitimate? I’ve never seen anyone use this construct ("FOO= ")
What happens when you do FOO=’’ ?
$ FOO= ruby -e ‘p ENV[“FOO”]’
nil
What does ruby -e 'p “” ’ do?
$ FOO=xyz ruby -e ‘p ENV[“FOO”]’
xyz
$

The problem is that ENV doesn’t contain the key FOO in
the 2nd case, where the value is zero-length.

What possible justification could there be for this behaviour?
I’m not sure … is this construct useful to you in some way? Setting an
environment variable to an empty string and reading it in Ruby?

M. Edward (Ed) Borasky wrote:

$ FOO= env |grep FOO
The problem is that ENV doesn’t contain the key FOO in

On Gentoo Linux:

[email protected] ~ $ export FOO=
[email protected] ~ $ set|grep FOO
FOO=
_=FOO
[email protected] ~ $ export FOO=""
[email protected] ~ $ set|grep FOO
FOO=
_=FOO
[email protected] ~ $ which ruby
/usr/bin/ruby
[email protected] ~ $ ruby -v
ruby 1.8.5 (2006-06-22) [i686-linux]
[email protected] ~ $ ruby -e ‘p “”’
“”
[email protected] ~ $ ruby -e ‘p ENV[“FOO”]’
“”
[email protected] ~ $ bash --version
GNU bash, version 3.1.17(1)-release (i686-pc-linux-gnu)
Copyright © 2005 Free Software Foundation, Inc.
[email protected] ~ $

Happy to help … at least it works the way I expected it to work on
Linux with Ruby 1.8.5 (pre something, IIRC). I still don’t see how it’s
useful, though. :slight_smile:

M. Edward (Ed) Borasky wrote:

On Gentoo Linux:

Yes, that’s the way it should work. Same on Debian with 1.8.4
It’s a bug on ruby/windows (I haven’t tried 1.8.4), because:

$ export FOO=
$ cmd
Microsoft Windows XP [Version 5.1.2600]
© Copyright 1985-2001 Microsoft Corp.
prompt>set FOO
FOO=
prompt>set BAR
Environment variable BAR not defined

So Windows, like *nix, knows that the environment variable is
set and that it has an empty value. I need to detect this in
Ruby the same as on Linux.

Is this legitimate? I’ve never seen anyone use this construct ("FOO= ")

It’s a shorthand way of saying “(export FOO=; command …)”, i.e.
export a value for an environment variable just for this command.

What happens when you do FOO=’’ ?

Exactly the same as not using ‘’, as you’d expect.

What does ruby -e 'p “” ’ do?

“”

just as expected, of course.

I’m not sure … is this construct useful to you in some way? Setting
an environment variable to an empty string and reading it in Ruby?

Absolutely. The variable provides an alternative to an internal default
value, and if it’s set, I want to use it by saying (ENV[“FOO”] ||
‘default’)
Works on Linux, breaks on Windows, contrary to Window’s own rules.

Clifford H…

M. Edward (Ed) Borasky wrote:

Hmmm … perhaps Ruby/Windows is stripping off or not stripping off an
end-of-line character inappropriately?

There are no end-of-line characters stored in environment variables,
so that shouldn’t be an issue.

This might be another piece of
Curt H.’ “mingw vs. Visual Studio for compiling Ruby” puzzle …
Does it work with the built-in Ruby from CygWin?

Hah - good call. The cygwin-compiled ruby works correctly, presumably
because it uses the cygwin “getenv” implementation. The bug’s only in
the native Windows version.

Clifford H…

Hi,

At Mon, 31 Jul 2006 15:37:02 +0900,
M. Edward (Ed) Borasky wrote in [ruby-talk:205137]:

Hmmm … perhaps Ruby/Windows is stripping off or not stripping off an
end-of-line character inappropriately? This might be another piece of
Curt H.’ “mingw vs. Visual Studio for compiling Ruby” puzzle …

Win32 API GetEnvirionmentVariable() doesn’t tell if the
variable is empty or unset. Actually, there is no way to set
an empty environment variable by cmd.exe. I’m not sure if it
is misfeature or restriction by design.

Clifford H. wrote:

M. Edward (Ed) Borasky wrote:

On Gentoo Linux:

Yes, that’s the way it should work. Same on Debian with 1.8.4
It’s a bug on ruby/windows (I haven’t tried 1.8.4), because:
Hmmm … perhaps Ruby/Windows is stripping off or not stripping off an
end-of-line character inappropriately? This might be another piece of
Curt H.’ “mingw vs. Visual Studio for compiling Ruby” puzzle …

Does it work with the built-in Ruby from CygWin? The reason I ask is
that when you install CygWin, it gives you the option of defaulting to
“Unix” or “Windows” end-of-line formats for some kinds of files. It
recommends Unix, which is what I’ve always picked. I do know there are
differences in the way CygWin Perl and ActiveState Perl handle
end-of-line characters.

Hmmm … perhaps Ruby/Windows is stripping off or not
stripping off an end-of-line character inappropriately?

I had the same problem a couple of weeks ago.

Use plain Windows, no Ruby, and you get the same kind of
results. It’s just not possible to set an environment variable
to an empty string, on Windows.

gegroet,
Erik V. - http://www.erikveen.dds.nl/


H:> set FOO=bar

H:> echo %FOO%
bar

H:> set FOO=

H:> echo %FOO%
%FOO%

H:> set FOO=’’

H:> echo %FOO%
‘’

H:> set FOO=""

H:> echo %FOO%
“”

On 7/31/06, [email protected] [email protected] wrote:

an empty environment variable by cmd.exe. I’m not sure if it
is misfeature or restriction by design.


Nobu Nakada

As far as I know, Windows uses an empty env. var. as a signal to
delete that env. var.

Curt

Hi,

At Mon, 31 Jul 2006 22:57:03 +0900,
Curt H. wrote in [ruby-talk:205183]:

Win32 API GetEnvirionmentVariable() doesn’t tell if the
variable is empty or unset. Actually, there is no way to set
an empty environment variable by cmd.exe. I’m not sure if it
is misfeature or restriction by design.

As far as I know, Windows uses an empty env. var. as a signal to
delete that env. var.

In accurate, Windows allows an empty env var, but cmd.exe (and
of course, command.com) uses it to delete it.

$ ./ruby -e ‘ENV[“FOO”]="";p system(“cmd /c set FOO”)’
FOO=
true

And, usa has fixed the empty value issue today.

On 7/31/06, [email protected] [email protected] wrote:

delete that env. var.

Nobu Nakada

Ok… thanks!

Curt

[email protected] wrote:

And, usa has fixed the empty value issue today.

Thankyou!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs