0

I'm writing a series of Ubuntu post-install scripts in order to install libraries and tools I need, one of this should install pyenv and latest python version using the former, the script is the following:

#!/usr/bin/env bash

# clone the github repo
echo "Installing pyenv..."
git clone https://github.com/yyuu/pyenv.git ~/.pyenv

# update .bashrc
echo '# pyenv settings:' >> ~/.bashrc
echo 'export PYENV_ROOT="~/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'export PATH="~/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

# reload
. ~/.bashrc

# install python and virtualenv
echo "Installing python 3.5.1 + virtualenv..."
pyenv install 3.5.1
pyenv global 3.5.1
pip install --upgrade pip
pip install virtualenv
echo "pyenv has been installed with python 3.5.1 and virtualenv"

The problem is that the reload (. ~/.bashrc) seems to not work as expected,because I get:

python.sh: pyenv: not found
python.sh: pip: not found

I also tried with source ~/.bashrc and exec $SHELL but the problem remains.

How can I solve the problem?

ps: I'm executing the script with: sudo sh python.sh

3
  • nope, I checked the .bashrc before launching the script and it's ok Commented Apr 30, 2016 at 15:42
  • This is no bash script if you start it with: sudo sh python.sh Commented Apr 30, 2016 at 16:01
  • @wallyk I think he wrote he tried that already. Commented Apr 30, 2016 at 16:07

2 Answers 2

1

If ~ is quoted, it does not expand to the user's home directory. Use this instead.

echo '# pyenv settings:' >> ~/.bashrc
echo 'export PYENV_ROOT=~/.pyenv' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'export PATH=~/.pyenv/bin:"$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

I would also rewrite it to look something like this:

cat <<'EOF' >> ~/.bashrc
# pyenv settings:
export PYENV_ROOT=~/.pyenv
export PATH=$PYENV_ROOT/bin:$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
EOF

However, you shouldn't be sourcing the user's .bashrc file; you don't know what else is in there, and you don't need to execute the whole thing, just the parts needed for the rest of your script. Replace . ~/.bashrc with

export PYENV_ROOT=~/.pyenv
export PATH=$PYENV_ROOT/bin:$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
Sign up to request clarification or add additional context in comments.

2 Comments

I rewrote the script but the problem persists :(
ok, by exporting only the variable that I need without the whole .bashrc reload it works! (I tried that previously but it failed... maybe due to ~)
0

Dangerous to manipulate ~/.bashrc, you don't need to either, unless you want something to be permanent.

Change

echo 'export PYENV_ROOT="~/.pyenv"' >> ~/.bashrc
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bashrc
echo 'export PATH="~/.pyenv/bin:$PATH"' >> ~/.bashrc
echo 'eval "$(pyenv init -)"' >> ~/.bashrc

to

export PYENV_ROOT="~/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
export PATH="~/.pyenv/bin:$PATH"
eval "$(pyenv init -)"

and the rest of the script will use the modified variables, even subshells.

EDIT

To make it permanent write it to a ~/.pyenvrc and just modify ~/.bashrc by appending

source ~/.pyenvrc

1 Comment

Difficult to do it from a script, unless you know how the .bashrc is laid out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.