0

This question already has an answer here:

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?

marked as duplicate by thrig, Gilles shell-script May 24 '17 at 23:15

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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"'

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