Environment variables with empty value missing from ENV?

C

Clifford Heath

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 Heath.
 
M

M. Edward (Ed) Borasky

Clifford said:
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

M. Edward (Ed) Borasky

M. Edward (Ed) Borasky said:
Clifford said:
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?
Clifford Heath.
On Gentoo Linux:


znmeb@DreamGate ~ $ export FOO=
znmeb@DreamGate ~ $ set|grep FOO
FOO=
_=FOO
znmeb@DreamGate ~ $ export FOO=""
znmeb@DreamGate ~ $ set|grep FOO
FOO=
_=FOO
znmeb@DreamGate ~ $ which ruby
/usr/bin/ruby
znmeb@DreamGate ~ $ ruby -v
ruby 1.8.5 (2006-06-22) [i686-linux]
znmeb@DreamGate ~ $ ruby -e 'p ""'
""
znmeb@DreamGate ~ $ ruby -e 'p ENV["FOO"]'
""
znmeb@DreamGate ~ $ bash --version
GNU bash, version 3.1.17(1)-release (i686-pc-linux-gnu)
Copyright (C) 2005 Free Software Foundation, Inc.
znmeb@DreamGate ~ $


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. :)
 
C

Clifford Heath

M. Edward (Ed) Borasky said:
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]
(C) 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.

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 Heath.
 
M

M. Edward (Ed) Borasky

Clifford said:
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 Hibbs' "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.
 
C

Clifford Heath

M. Edward (Ed) Borasky said:
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 Hibbs' "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 Heath.
 
N

nobu

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 Hibbs' "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.
 
E

Erik Veenstra

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%
""

----------------------------------------------------------------
 
C

Curt Hibbs

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 Hibbs' "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.

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

Curt
 
N

nobu

Hi,

At Mon, 31 Jul 2006 22:57:03 +0900,
Curt Hibbs wrote in [ruby-talk:205183]:
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.
 
C

Curt Hibbs

Hi,

At Mon, 31 Jul 2006 22:57:03 +0900,
Curt Hibbs wrote in [ruby-talk:205183]:
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.

Ok... thanks!

Curt
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top