J Y J Y - 1 month ago 16
Bash Question

How can I change the shell directory in the C language program?

When I use chdir() to change the current directory, the program will not change when the program is shut down. So How can I apply chdir() to Shell after using chdir() and programs have been terminated?

Answer

You cannot do that (changing your parent shell directory from inside a C program). The current directory is an attribute of every process, and your shell runs in a different process than your program (so the shell process and your program's process have each their own current directory).

Read Advanced Linux Programming. It has several chapters related to your issue.

Perhaps you might add some shell function (e.g. into your ~/.bashrc ...) which perhaps could use eval to run your C program (which would output some cd command, that the eval or source builtin of your shell would handle within the same shell process); BTW ssh-agent might be inspirational. Actually I don't recommend this route if you are a newbie.

PS. You really should motivate your question and give a lot more context; it smells badly like an XY problem.