How would you arrange for a shell script to be executed in a child process, while still being able to change the current directory of the invoking shell?


You can not.

The environment of the parent shell is copied to the child process when the child is started, and from that point on, they are distinct and utterly separate. The child process will not be able to directly modify the environment of the parent.

The only way for the child to influence its parent is for it to send a message to the parent and for the parent to act on it:

eval "$( bash -c 'echo "cd /"' )"  # message through command string to stdout


# message through USR1 signal triggers predefined action

trap 'cd /' USR1
bash -c 'kill -s USR1 "$PPID"'

Not the answer you're looking for? Browse other questions tagged or ask your own question.