0

I was working on a script to set up a project-specific environment. I created a script to set up the directory stack as suggested in the first comment to this question. Console output provided evidence that the stack was being created as designed, but as the script completed, the stack did not exist when I was returned to the console.

I suspect that this is a design feature to allow the script writer an independent environment, and that this touches on a question of broader scope concerning how the environment is handled inside and outside of a script. What is going on and where can I learn more?

2NinerRomeo
  • 925
  • 2
  • 7
  • 13
  • Did you *run* the script or *source* it? – steeldriver Feb 04 '21 at 17:39
  • I ran the script. – 2NinerRomeo Feb 04 '21 at 17:40
  • ... so try sourcing it instead - see relate [Script to change current directory (cd, pwd)](https://unix.stackexchange.com/questions/27139/script-to-change-current-directory-cd-pwd) – steeldriver Feb 04 '21 at 17:41
  • Source seems to do the job. That gives me a hint on where to look next. – 2NinerRomeo Feb 04 '21 at 17:41
  • This tells me that the difference that source executes in the current chell process, while executing starts a new process: https://superuser.com/questions/176783/what-is-the-difference-between-executing-a-bash-script-vs-sourcing-it – 2NinerRomeo Feb 04 '21 at 17:44
  • 2
    As this is due to the same reason `cd` (or changing the current environment in other ways) in a script that is run in a separate child process can't affect the parent shell's environment, I'm closing this as a duplicate of https://unix.stackexchange.com/questions/27139/script-to-change-current-directory-cd-pwd – Kusalananda Feb 04 '21 at 17:49

0 Answers0