Quote:
Originally Posted by sneakyimp
I'm wondering what effect that cd command might have.
|
directory changes... stay with the current shell and are inherited by child or sub shells.
Let's say I make a script with the following to demonstrate:
Code:
echo $PWD
cd ~/
echo $PWD
(
cd ..
echo $PWD
)
echo $PWD
Assuming when I run the script that my $PWD is /,
I would expect this output:
Code:
/
/home/miati
/home
/home/miati
Which ends up being correct.
Examine that and why each one is as it is. Specifically, why the last one != the second to last one (remember, going into a child shell will cause the child shell to inherit the current directory but discard any changes once it exits)
A really easy way to test this is by typing this in the terminal:
Your current directory doesn't change, yet you know it did change directory in the subshell since it listed /'s contents.
Quote:
Can I reasonably expect this to be the case on all *nix platforms?
|
You are most likely using bash, so this is bash behavior. If other *nix platforms use something besides bash, this behavior could change.
However you could easily load up a bash shell (type in bash in the shell)
OR - type #!/bin/bash
This indicates to the script reader that it should be interpreting it as bash script (my understanding of this is less clear.)
Quote:
cd commands only last for the duration of the current script and do not affect the cwd in other..
|
Since starting a script opens a child shell, this is following that behavior.