2

I have a question about following lines related to adding PATH to enviroment.

export PATH=/usr/loca/cuda/bin:$PATH
export PATH=/usr/local/cuda-9.1/bin${PATH:+:${PATH}}
export PATH="/home/ics_vr/anaconda3/bin:$PATH"
export PATH="$PATH:/home/user/anaconda3/bin"

Regardless the content of path in each export lines, my first question is how do I distinguish thoes lines starting with export PATH=? e.g. grammer and its functions, regardless the variable I used in thoes lines.

Secondly, I see many people use # to comment on/off to switch those path,but this is not convenient. Is there any union way to realize all, without commenting the export line every time?

This is convenient because people want to use system python for example as default, but if the path is settled not properly, anaconda python interpreter will be settled by default. We need a way that default is the system python interpreter, and when I need anaconda, I will use

source activate ENV_I_BUILD

Thank you for your time and help. I am very appreciate on that.

waschbaerYOYO
  • 801
  • 1
  • 7
  • 8

1 Answers1

4

The environment variable PATH is a list of colon separated folder paths where to find executables.

The order in which folder paths are places in this variable is very important. Indeed, if you call a program from the command line, the executable will first be searched in the first folder path, then if it's not there the second and so on...

Anaconda ships with a python installation (either 2.x or 3.x). If you export:

export PATH="/home/ics_vr/anaconda3/bin:$PATH"

then the python in "/home/ics_vr/anaconda3/bin/anaconda3" will be used preferentially. Thus, if you want to keep the system python by default, you might want to use:

export PATH="$PATH:/path/to/whatever/conda"

The source activate ... will prepend the environment bin folder in your PATH anyway. So if you activate an environment, the system python will be superseeded by the python of the conda env.

As for the two lines:

export PATH=/usr/loca/cuda/bin:$PATH
export PATH=/usr/local/cuda-9.1/bin${PATH:+:${PATH}}

you will have to decide what executables you want first in your PATH variable.

For information, you can set multiple folders in your PATH in one line:

export PATH="$PATH:/usr/loca/cuda/bin:/home/ics_vr/anaconda3/bin:/my/personal/bin"

Do not forget to add what was already in your PATH variable when exporting a new PATH if you do not want to loose basic commands listed in, for example, "/usr/bin" or "/usr/local/bin".

Eric C.
  • 3,310
  • 2
  • 22
  • 29
  • By your last suggestion: do you mean export PATH="$PATH:/an_another_path:$PATH" is not the correct way to export? We should at least write each export line with "/usr/bin" or "/usr/local/bin" at the beginnint, and plus an_another_path? – waschbaerYOYO Mar 09 '18 at 16:05
  • I have a PATH problem, do you mind to have a look to this [link](https://stackoverflow.com/questions/49195179/how-to-solve-the-conda-virtual-env-and-system-env-conflict-on-import-cv2) – waschbaerYOYO Mar 09 '18 at 16:07
  • I meant either `export PATH=$PATH:something` or `export PATH=something:$PATH`. – Eric C. Mar 09 '18 at 16:08
  • Can someone explain this the `${PATH:+:${PATH}}` part in `PATH=/usr/local/cuda-9.1/bin${PATH:+:${PATH}}`? I know WHAT is does, but the syntax is not clear for me. – Eduardo Reis Jun 01 '22 at 01:12
  • 1
    @EduardoReis https://stackoverflow.com/questions/40771781/how-to-append-a-string-in-bash-only-when-the-variable-is-defined-and-not-null – Eric C. Jun 02 '22 at 09:55
  • 1
    @EricC. Thank you for the reference. In the case used above, it is used the `${VAR:+:${VAR}}` format. It took a while for me to realize that the first `:` is from the `${VAR:+thing_to_append}` format, and the second `:` is part of the `thing_to_append` that will actually be appended. I just realized it as I was writing this comment. Thank once again. – Eduardo Reis Jun 03 '22 at 14:21