Dan Armbrust wrote:
> Thanks for all the feedback - this turned out to be quite informative.
> 
> So, I seem to have the following 2 methods:
> 
> P1=$(\dirname "$(\which "$0")")
> 
> 
> P2=$(cd "$(\dirname "$0")"; pwd)
> 
> And they both seem to work in all the cases that I'm worried about,
> anyway.  And if I followed the thread correctly, method two is better,
> because it doesn't depend on any behavior from "which" - and that is
> good, because which is not necessarily consistent across all
> platforms.
> 
> However, since I barely know enough shell scripting to be dangerous,
> can someone please explain to me why the second method works?  Why
> doesn't the cd command leave me in a different place than where I
> started, if I execute the script from another folder?  Does cd get
> executed twice?
> 

Commands inside the $() are executed in a subshell (I'm not sure if it 
actually runs a separate process or just behaves like one.) Once the 
subshell is done executing all the environment changes get discarded.

-Steve