[Novalug] How can script know if it's been 'sourced' rather than 'called'?

Michael Stone mstone at mathom.us
Fri Apr 6 15:01:59 EDT 2007


On Fri, Apr 06, 2007 at 02:42:59PM -0400, Ben Creitz wrote:
>I have little script which starts ssh-agent for a given period of time
>and loads a private key.  The script must be "sourced" with source or
>. in order to work, so that the calling shell can access the env vars
>that ssh-agent exports.

Better to just print the variables and let the caller evaluate them, 
then it can work with different shells. (e.g., csh) Otherwise, there's 
no really good way to do what you're trying to do unless you make a 
whole lotta assumptions about the environment (e.g., you can look at $_ 
to see what you're being called as, $PS1 to see if you're an interactive 
shell, $SHELLOPTS, etc.) Each of those will have some corner cases where 
they'll fail, though--and if you're making all those assumptions it's 
easier to just assume that nobody will do that. :)

Mike Stone


More information about the Novalug mailing list