Re:Making Hard Things Easy
by ustad on Friday October 06, 2023 @01:10PM
(#37793355)
Wait, how do you “cd to it” from within the script? Doesn’t exiting the script take you back to where you were?
[ Parent
| on HN ]
Re:Making Hard Things Easy
by justin_oaks on Friday October 06, 2023 @01:47PM
(#37793777)
You're right that you often can't modify your current environment by invoking a shell script. That's because it's executed in a sub-shell.
For cases where you need to modify your current environment (setting environment variables, changing directories, etc), you need to run the script using the "source" built-in. That will execute the script in the current shell rather than a sub-shell.
So instead of
./some-script.sh
you'd run source some-script.sh
or use the dot (".") shorthand . some-script.sh
In cases where I need to source a script, I generally create an alias or a shell function for it. Otherwise I may forget to source it.
Re:Making Hard Things Easy
by fiddlerwoaroof on Friday October 06, 2023 @02:49PM
(#37794645)
For this I use shell functions in my .zshrc and I wrote a loader to source a bunch of files in a .zsh.d directory.
Rolled your own direnv?
Re:Making Hard Things Easy
by fiddlerwoaroof on Friday October 06, 2023 @08:10PM
(#37797905)
No, this is global: I use direnv too.
It’s more like “roll your own oh-my-zsh”