Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

Wait, how do you “cd to it” from within the script? Doesn’t exiting the script take you back to where you were?


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.


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?


No, this is global: I use direnv too.

It’s more like “roll your own oh-my-zsh”




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: