tags:

views:

235

answers:

2

Hi,

My .bashrc looks something like this...

 export PERL5LIB="/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0/${PLAT}-thread-multi"
 export PERL5LIB="${PERL5LIB}:/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0"

 function dev {
   export PERL5LIB="/dev/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0/${PLAT}-thread-multi"
   export PERL5LIB="${PERL5LIB}:/dev/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0"
 }

The problem is that when I grep for PERL5LIB is see everything.

> env | grep PERL
PERL5LIB=/tools/perl/Linux/x86_64/lib/perl5/5.10.0/x86_64-thread-multi:/tools/perl/Linux/x86_64/lib/perl5/5.10.0
 export PERL5LIB="/dev/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0/${PLAT}-thread-multi";
 export PERL5LIB="${PERL5LIB}:/dev/tools/perl/Linux/${PLAT}/lib/perl5/5.10.0";

So it's picking up the stuff inside of my "dev" function. Is there a way to hide the contents of a function? Or do I just need to get used to getenv.. Old habits are hard to break..

+1  A: 

Run type env at your bash prompt, and provide the output; for me, this indicates that env is /usr/bin/env, a separate executable; such executables have no way to know anything about functions or non-exported variables.

That said, without fixing the underlying problem (the likely cause being use of a bash built-in, function or alias in place of /usr/bin/env, which the output of the type command will show), there's a workaround available: env | grep '^PERL'; the carrot will emit only output starting at the beginning of a line, and function contents are indented in output of set (which appears to be running in place of env; again, type env should give a clue to the cause).

One point of clarification: set is a bash builtin which, when run with no arguments, dumps defined variables (environment or otherwise) and functions; when run with arguments, it has some other, completely different (and POSIX-specified) behaviors. env, as an external program, has no access to unexported variables or to functions defined within the shell that calls it.

(set is actually not bash-specific, but rather is specified by POSIX to dump all shell variables; its additional functionality of dumping function definitions is to my knowledge an extension beyond the letter of the standard).

Charles Duffy
env is /usr/local/bin/env
rh0dium
Can you explain your second comment WRT "output of set"? Thanks
rh0dium
...and that's output from `type`, not `which`? If what's being invoked truly is `/usr/local/bin/env`, it has no way of knowing what your defined shell functions are, and thus no way of dumping them.
Charles Duffy
You led me to the answer! Thanks!!
rh0dium
+1  A: 

Try:

( set -o posix ; set )
Chen Levy
I suppose you could have some fun with functions and aliases to have `env` always invoke that sequence when called without arguments -- but as I understand it, the goal here is to come up with a way for rh0dium to get the desired behavior *without changing his habits* (and thus the commandline he habitually pipes).
Charles Duffy