How to let Bash script extends User's PATH?

At command line, my command python3 -u jupterlab notebook works perfectly as python locates at /srv/conda/envs/notebook/bin/python3.

Next, I have a bash script, say /usr/local/share/python3-login, its content is

#!/bin/bash -l
echo $PATH
exec python3 -u "$@"

My problem is when I call the script, I encountered an error where python3 not found /usr/local/share/python3-login: line 3: exec: python3: not found

I tried to debug by adding echo $PATH at line 2, and turned out PATH is /opt/conda/condabin:/usr/local/bin:/usr/bin:/bin:/usr/local/games:/usr/game, which python3 doesn't exist

How to let my bash script recognize /srv/conda/envs/notebook/bin/python3?

To add more context, I built a docker image with Ubuntu OS

3 answers

  • answered 2021-11-29 04:24 scavenger

    -l Make bash act as if it had been invoked as a login shell That means it will loads the various profile files as if you just logged in

    Since /srv/conda/envs/notebook/bin does not look as a standard path one would see in an profile file, I suspect you do something more to get this in your $PATH in the first place.

    solution 1: simply add whatever you do to get this path in your environment, into the script.

    solution 2: simply don't use the -l argument in your shebang.

  • answered 2021-11-29 04:24 do-no-van

    You can just use the full path:

    #!/bin/bash -l
    echo $PATH
    exec /srv/conda/envs/notebook/bin/python3 -u "$@"
    

  • answered 2021-11-29 04:26 Saiansh Singh

    To extend the user's path, modify the PATH environment variable.

    export PATH=/srv/conda/envs/notebook/bin/python3:$PATH
    

How many English words
do you know?
Test your English vocabulary size, and measure
how many words do you know
Online Test
Powered by Examplum