I am attempting to write a bash script that will install rbenv from beginning to end. I am on a Mac for interest sake.

But there are some things that are not working mainly i am changing the directory but only in the subshell.

echo "installing rbenv ruby manager manager"
cd ~
git clone git://github.com/sstephenson/rbenv.git .rbenv
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
source .bash_profile

echo "installing ruby build for rbenv"
git clone https://github.com/sstephenson/ruby-build.git
cd ruby-build
sudo ./install.sh

echo "rbenv and ruby-build have been installed, installing ruby now.."
sleep 2

read -p "would you like chronospere to install ruby 1.9.3 [y/n]" RESP
if [ "$RESP" = "y" ]; then
  rbenv install 1.9.3-p327
  rbenv rehash
  rbenv global 1.9.3-p327
  ruby -v
  echo "alrigt skipping.. vagrant has rbenv installed you can install ruby it at your leisure "
  echo "hold cmd, and double clck https://github.com/sstephenson/rbenv for more info"

cd ~

So it seems as if installing it works. But most of my code does not get run, or doesn't get run in the place I'd like it to.

vagrant@precise64:/vagrant$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [x86_64-linux]
vagrant@precise64:/vagrant$ rbenv
The program 'rbenv' is currently not installed.  You can install it by typing:
sudo apt-get install rbenv

As you can see its as if nothing was installed. The source .bash_profile never happened

vagrant@precise64:/vagrant$ cd      
vagrant@precise64:~$ cd ~ 
vagrant@precise64:~$ source .bash_profile 

After I manually source the bash_profile,

vagrant@precise64:~$ rbenv
rbenv 0.4.0-45-g060f141
vagrant@precise64:~$ rbenv versions
  * system (set by /home/vagrant/.rbenv/version)
vagrant@precise64:~$ rbenv global 1.9.3-p327
vagrant@precise64:~$ ruby -v
ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-linux]

So as you can see its as if the code is running just not moving from one directory to the next. I don't even know if this is possible with in a bash script. I move from ~ / (HOME) to /ruby-build back to ~ then to /vagrant. Is this possible in a single bash script. this is weird because if I open the terminal and run that code line by line it works 100%.

  • Unless you source your install script, the things sourced in there will not be propagated to the parent shell.
    – l0b0
    May 30, 2013 at 9:28
  • how can i then source the parent shell from the script or is there a way to maybe end the script source the parent shell and then re-enter the script??
    – TheLegend
    May 30, 2013 at 9:53
  • I think you need to decompose this into smaller chunks and ask simpler questions. See for example Why doesn't “cd” work in a bash shell script?
    – l0b0
    May 30, 2013 at 10:44

1 Answer 1


The most direct reason is mentioned by @l0b0: you need to update the PATH variable of your current shell, which you do with source .bash_profile (or . .bash_profile). Any other shell spawned after you have updated shell's initialisation file by your script will work "out-of-the-box" (because it's going to use the updated init file).

That said, unless you want to run into a terrible mess sooner or later, you probably want to tidy this up a bit. The main troubles I see, are:

  1. use some package management whenever possible - as a Mac user you might want to look at a related question on the SuperUser SE. Not using package management is generally a bad practice (although there are exceptions to this rule).

  2. Is sudo ... guaranteed to work? Do you really need to install it under root, given that you have rbenv in your home directory? This is connected to the previous point.

  3. while ~/.rbenv/bin is probably fine, it is usually better to mimick the general FHS in your own directory (either by having bin/, etc/, lib/ etc. directly there or by putting it into a separate subdirectory).

  4. you might want to use set -e to abort the script on any errors (for which you are not explicitely checking) - see help set or man bash for more details. git clone is the first candidate here - if the repository already exists, the clone fails but the script silently accepts that.

  5. >> ~/.bash_profile is likely to pollute your shell's init file. While it is possible to check whether the lines are already there, barely printing a note reminding about the neccessity of adding these manually for the installation to work properly might actually be more useful. Especially once you decide to give the script to somebody else.

Not the answer you're looking for? Browse other questions tagged or ask your own question.