You would need to invoke parentScript
by sourcing it as well:
. parentScript
Then, in whatever script contains that, you would need to make sure the first argument is
./grandparentScript CD_TO_USER_NAME_OPTION
A script invoked by any other means besides sourcing is run in a new process. A process has its own current working directory (man 3 getcwd). That directory is inherited from the parent process, but the parent doesn't get it from the child when the child exits. The only way to have an inner script change the working directory of an outer script is by running them in the same process. That is done most simply by sourcing, or the .
command, as you've discovered.
Another solution would be to use a shell function for your directory change:
magicCd() {
cd my/special/place
}
However, to avoid mixing procedural code with data, maybe the best choice would be simply to use the builtin cd
command and store the desired destination in a variable.
my_special_place="$HOME/my/special/place"
cd "$my_special_place"
This last is just as abstract as the sourced script, function or alias, and much more obvious to any maintenance programmer who comes along.