3

I am trying to set the load path for Pyenv in my server .bashrc file.

I am following this tutorial where it asks us to set pyenv to the load path

However, in my .bashrcfile, I already see the below commands

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
if command -v pyenv 1>/dev/null 2>&1; then
 eval "$(pyenv init -)"
fi

And how is it different from the below provided in the tutorial shared above?

export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

May I know what does if...fi block does in the code shown above?

The Great
  • 7,215
  • 7
  • 40
  • 128
  • The `if` block just makes it so the `~/.bashrc` will continue to function without error, even if you uninstall pyenv – jordanm Aug 26 '20 at 16:10
  • Hi, can I know what does this exactly mean? `command -v pyenv 1>/dev/null 2>&1; then eval "$(pyenv init -)"`? what does `dev/null` or `2>&1` mean? can help me translate this into words? – The Great Aug 27 '20 at 04:13

1 Answers1

3

It's mostly bash's syntax.

#1.

export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"

is equivalent to

export PATH="$HOME/.pyenv/bin:$PATH"

as in the first case, you're declaring a variable named PYENV_ROOT then using it.

#2.

if and fi are how you write if-statements in bash.

#3.

command -v pyenv is used to execute a command (pyenv) in this case, the -v option prints the pathname e.g.

$ command -v python
/usr/bin/python
if command -v pyenv 1

means that if the command pyenv is found, then execute eval "$(pyenv init -)"

#4.

Here, >/dev/null 2>&1; is used to discard the output. read more about it this answer.

Hence, two blocks of code are almost same, the only differences are: the first one has a if-block and second one has one extra command eval "$(pyenv virtualenv-init -)".

kHarshit
  • 11,362
  • 10
  • 52
  • 71
  • I was searching for this Q and A. It helped me very much. Also I was not sure how to put 3rd 4th 5th lines of blockquote 1 into terminal/konsole. Searching the net I got that from terminal you have to use 'echo' with '-e' option with '-e' option's qualifier '\n' OR you have to nano the .bashrc. Am I right? Thank you very much. – banuyayi Feb 19 '23 at 17:10