You are not logged in.

#1 2014-04-24 11:47:31

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

[SOLVED] bash - diff btwn 'normal' environment variables and VAR=x cmd

I always assumed variables passed like this:

VAR=value cmd

are passed to the program in the same way as environment variables. However; this is not the case; who can explain this?

$ svn commit
svn: E205007: Commit failed (details follow):
svn: E205007: Could not use external editor to fetch log message; consider setting the $SVN_EDITOR environment variable or using the --message (-m) or --file (-F) options
svn: E205007: None of the environment variables SVN_EDITOR, VISUAL or EDITOR are set, and no 'editor-cmd' run-time configuration option was found
$ VISUAL=$VISUAL svn commit
Sending        file
Deleting       file
Adding         file
Transmitting file data ..

Last edited by Spider.007 (2014-04-24 17:55:49)

Offline

#2 2014-04-24 12:54:24

drcouzelis
Member
From: Connecticut, USA
Registered: 2009-11-09
Posts: 4,092
Website

Re: [SOLVED] bash - diff btwn 'normal' environment variables and VAR=x cmd

That... looks really strange.

What is $VISUAL set to when you open a new shell?

Offline

#3 2014-04-24 13:04:45

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] bash - diff btwn 'normal' environment variables and VAR=x cmd

I'm curious too.  I could at least *speculate* on how it could go wrong the other way around: if the envoked process spawns a new process that inherits a more general environment than the parent's, the first could work and the second could fail - but these results indeed should not happen.

Test code as evidence that the variables are passed in the same way:

#include <stdio.h>

int main(int argc, char **argv, char **env) {
	char **envp;
	for (envp = env; envp && *envp; envp++) printf("%s\n", *envp);
	return 0;
}

Basically this is a very basic `env'.  It shows all environment variables known to the process.  No matter how they are set, they are all placed in env.

NOTE: most programs will use getenv() rather than the extra parameter to main(), but this is meant to test what is passed where.

Last edited by Trilby (2014-04-24 13:07:21)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#4 2014-04-24 13:07:40

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: [SOLVED] bash - diff btwn 'normal' environment variables and VAR=x cmd

It's not strange if VISUAL is set but not exported with the 'export VISUAL' command; then it is not passed to the command execution environment.
With the syntax 'VISUAL=$VISUAL svn commit', you create a copy of the not exported variable, with the same name, but inside the command execution environment; so it works then.

Offline

#5 2014-04-24 13:14:43

Trilby
Inspector Parrot
Registered: 2011-11-29
Posts: 29,442
Website

Re: [SOLVED] bash - diff btwn 'normal' environment variables and VAR=x cmd

I'll be ...  I just confirmed the above with my test code.  And I just learned something new and important (thanks berbae).

Last edited by Trilby (2014-04-24 13:15:00)


"UNIX is simple and coherent..." - Dennis Ritchie, "GNU's Not UNIX" -  Richard Stallman

Offline

#6 2014-04-24 13:32:11

berbae
Member
From: France
Registered: 2007-02-12
Posts: 1,302

Re: [SOLVED] bash - diff btwn 'normal' environment variables and VAR=x cmd

$ VARTEST='abcd'
$ cat >./cmdtest
#!/bin/bash
echo "VARTEST=***$VARTEST***"
$ chmod 755 ./cmdtest
$ ./cmdtest
VARTEST=******
$ export VARTEST
$ ./cmdtest
VARTEST=***abcd***

and

$ unset VARTEST
$ VARTEST='abcd'
$ ./cmdtest
VARTEST=******
$ VARTEST=$VARTEST ./cmdtest
VARTEST=***abcd***

Offline

#7 2014-04-24 17:55:05

Spider.007
Member
Registered: 2004-06-20
Posts: 1,175

Re: [SOLVED] bash - diff btwn 'normal' environment variables and VAR=x cmd

Thanks berbae, I learned something new. You are right; the VISUAL variables is not set as 'exported' and is therefore not passed to new commands. I never realised this works like that, but it does. `set` displays all variables regardless of locality.

Offline

Board footer

Powered by FluxBB