making vars in bash script accessible from shell

Price, Erik eprice at ptc.com
Thu Nov 7 15:59:41 EST 2002


> -----Original Message-----
> From: Derek Martin [mailto:gnhlug at sophic.org]
> Sent: Thursday, November 07, 2002 3:37 PM
> To: GNHLUG mailing list
> Subject: Re: making vars in bash script accessible from shell
> 
> 
> -----BEGIN PGP SIGNED MESSAGE-----
> Hash: SHA1
> 
> At some point hitherto, Price, Erik hath spake thusly:
> When you execute a shell script, you fork a new copy of the shell.
> Anything you do in the child shell is not visible to the parent shell.

Doh.  I knew this too, I just forgot what it implies for shell programming.

> There is a way around this, which is to source the shell script,
> rather than execute it.

What exactly happens when you source a script (as opposed to executing it)?

If you can believe it I can't find the man page for bash on my system, sorry for asking these questions.

> Another way would be to have the shell script echo the variables, and
> then use eval to set them, like this:

Maybe as a last resort.  I still want to try to get it to work the original way (this is from the Bash Prompt HOWTO).

What I'm doing is using the PROMPT_COMMAND variable to execute the shell script.  Well, I was, now I've changed that to sourcing the shell script so that I can get the current environment to inherit the variables.

The problem is that this seems to only work at the moment I re-source .bashrc (which is where I've set PROMPT_COMMAND).  If I understand correctly, the contents of PROMPT_COMMAND are supposed to be executed every time a prompt is generated.  I have confirmed this by putting "date" in the PROMPT_COMMAND variable.  As expected, the time changes each time a new prompt is generated.

I suppose I need to escape the value of PROMPT_COMMAND so that it is not executed when I source .bashrc but rather is executed when PROMPT_COMMAND executes it, but how to do that?


Erik



More information about the gnhlug-discuss mailing list