Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

How do I {set an environment variable, change directory} inside a program or shell script and have that change affect my current shell?

0
Posted

How do I {set an environment variable, change directory} inside a program or shell script and have that change affect my current shell?

0

In general, you can’t, at least not without making special arrangements. When a child process is created, it inherits a copy of its parent’s variables (and current directory). The child can change these values all it wants but the changes won’t affect the parent shell, since the child is changing a copy of the original data. Some special arrangements are possible. Your child process could write out the changed variables, if the parent was prepared to read the output and interpret it as commands to set its own variables. Also, shells can arrange to run other shell scripts in the context of the current shell, rather than in a child process, so that changes will affect the original shell. For instance, if you have a C shell script named “myscript”: cd /very/long/path setenv PATH /something:/something-else or the equivalent Bourne or Korn shell script cd /very/long/path PATH=/something:/something-else export PATH and try to run “myscript” from your shell, your shell will fork and run the s

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123