Web lists-archives.org

Re: [Mingw-users] How can I tell whether my prog is running on cmd.exe or bash?




On Thursday 07 February 2008 12:30, Paul Leder wrote:
> > There's currently one fly in the ointment, though. I
> > noticed last night that 'getenv' is not picking up SHELL. Any other
> > environment variable you care to name, but not SHELL, despite 'set'
> > confirming that it exists. I'll try to work that one out as soon as
> > I finish the displacement activities...

This one is easy: `set' is only part of the story, when working with a 
Unixy shell.  There are two categories of environment variables: local 
and exported; `set' shows you both, but only those which are exported 
are passed in the environment inherited by child processes.  SHELL is 
defined as local by default.

  $ cat foo.c
  #include <stdio.h>
  #include <stdlib.h>
  int main()
  {

    char *shell = getenv( "SHELL" );
    printf( "shell is %s\n", shell ? shell : "unknown" );
    return 0;
  }

  $ gcc -o foo foo.c

  $ ./foo
  shell is unknown

  $ export SHELL

  $ ./foo
  shell is D:/usr/MSYS-1.0.11/bin/sh

> Sorry to go on about this...
>
> There's no SHELL in MinGW's 'environ'. To be precise, I've written a
> small test prog that prints out the contents of stdlib's 'environ'.
> This is what I'm seeing, running everything from a Cygwin bash shell:
> 
> [code snipped]

This will show you only the variables which have been exported by the 
shell; the ones you don't see are local to the shell.  (Variables set 
by cmd.exe, or from the Woe32 control panel are always exported, so you 
will always see them).  To have variables such as SHELL visible to all 
processes invoked from a bash shell, you should add `export VARNAME' 
to /etc/profile, (or your $HOME/.profile, or $HOME/.bash_profile).

Regards,
Keith.

-------------------------------------------------------------------------
This SF.net email is sponsored by: Microsoft
Defy all challenges. Microsoft(R) Visual Studio 2008.
http://clk.atdmt.com/MRT/go/vse0120000070mrt/direct/01/
_______________________________________________
MinGW-users mailing list
MinGW-users@xxxxxxxxxxxxxxxxxxxxx

You may change your MinGW Account Options or unsubscribe at:
https://lists.sourceforge.net/lists/listinfo/mingw-users