4 ms·
Wait, how do you “cd to it” from within the script? Doesn’t exiting the script take you back to where you were?
by ustad 3y ago
Wait, how do you “cd to it” from within the script? Doesn’t exiting the script take you back to where you were?
- justin_oaks 3y agoYou'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.
- fiddlerwoaroof 3y agoFor this I use shell functions in my .zshrc and I wrote a loader to source a bunch of files in a .zsh.d directory.
- tripleo1 3y agoRolled your own direnv?
- fiddlerwoaroof 3y agoNo, this is global: I use direnv too. It’s more like “roll your own oh-my-zsh”