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.
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
you'd run or use the dot (".") shorthand 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.