Skip to content

Instantly share code, notes, and snippets.

@benolee
Created October 20, 2010 16:44
Show Gist options
  • Save benolee/636789 to your computer and use it in GitHub Desktop.
Save benolee/636789 to your computer and use it in GitHub Desktop.

RVM and Bash

Post-installation line

The rvm installation documentation instructs you to put the following line at the very end of your bash profile:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.

For those, like me, who are not very familiar with bash scripting, this is what the line above does: [[ condition ]] evaluates the condition inside the double brackets and returns true or false. The option -s file tells bash to look for file and decide whether it exists and is nonempty. So the line [[ -s "$HOME/.rvm/scripts/rvm" ]] looks inside the default rvm scripts folder ~/.rvm/scripts/ for rvm, and returns true or false.

Next, the && operator (a logical AND with short-circuit) executes the rest of the line if and only if the previous test returns true. This means that in our rvm example, if the file ~/.rvm/scripts/rvm is not found (or is empty), then bash ignores the rest of the line. (ASIDE: I've seen the && used a lot when installing software from source in linux for make && sudo make install -- if make fails, then it doesn't try to install it to your system)

Finally, the bash command . file is identical to the source file command: it reads the file and executes any bash commands it finds. So, . "$HOME/.rvm/scripts/rvm" just executes the rvm bash script.

TL;DR

[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" looks for the rvm script and runs it.

rvm and .bashrc

The rvm install troubleshooting notes at http://rvm.beginrescueend.com/rvm/install/ explain how to change your bash profile to work with rvm. In Ubuntu, the default .bashrc contains the following line: [ -z "$PS1" ] && return

[ -z "$PS1" ] is true if the string "$PS1" has zero length (a non-interactive shell). rvm needs to load for both interactive and non-interactive shells.

rvm bash completion

[[ -r $rvm_path/scripts/completion ]] && . $rvm_path/scripts/completion

rvm in your prompt

PS1="\$(~/.rvm/bin/rvm-prompt) $PS1" You can also use the environment variables $RUBY_VERSION and $gemset_name instead.

.rvmrc and gemset loading

rvm only looks for a .rvmrc file when you change directories. If you open a new terminal tab while you're in the project root directory, rvm doesn't load the gemset and ruby version until you cd back into the folder. To force rvm to load the .rvmrc, add this line to the end of ~/.bashrc : [[ -r "$PWD/.rvmrc" ]] && . "$PWD/.rvmrc"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment