When you execute the script you initiate a subshell; variable assignments made in the subshell are 'lost' when the subshell exits (in this case the poetry_activate script exits).
As you've found, when you source the script (. poetry_activate or source poetry_activate) then no subshell is initiated and instead the commands (within the script) are executed within the current shell.
To eliminate the subshell, while also removing the need to source the script, you could replace the shell script with a function, eg:
poetry_activate() { source $(poetry env info --path)/bin/activate; }
# or
poetry_activate() {
source $(poetry env info --path)/bin/activate
}
NOTES:
- in the first example the trailing
; is required to separate the code from the closing }
- add this to your
.profile or .bashrc
- assumes your
PATH has been configured so the OS can find poetry
Now instead of referencing the script you reference the function; an example of referencing the function from the command line:
$ poetry_activate
.bash_rcor other similar.file so it is automatic. You'll have to figure out which one works best in your use-case. Seeman bashand search for INVOCATION. Good luck.