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?
Asked
Active
Viewed 2,118 times
1 Answers
1
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
or
# message through USR1 signal triggers predefined action
trap 'cd /' USR1
bash -c 'kill -s USR1 "$PPID"'

Kusalananda
- 333,661