public
Last active — forked from jimweirich/gist:343526

  • Download Gist
gistfile1.txt
1 2 3 4 5 6 7 8 9 10 11 12 13
# Switch to Ruby 1.8.7
rvm use 1.8.7
 
# Print out the ruby version
ruby -v
 
# But the output of this is:
#
# $ ./tryrvm
# <i> Now using ruby 1.8.7 p249 </i>
# ruby 1.8.6 (2010-02-05 patchlevel 399) [i686-darwin9.8.0]
#
# Why?
scripting_example.sh
Shell
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
#!/usr/bin/env bash
 
# rvm essentially has 2 methods of invocation:
#
# 1. script, ~/.rvm/bin/rvm is a *script* to execute rvm commands outside of the env
# environment. This method is an 'external script' and thus RVM would not be able
# to manage the parent shell's environment.
#
# 2. function, sourcing scripts/rvm loads rvm() as a shell *function* into a
# specific environment. Doing this will allow RVM to manage the shell environment.
#
# If the above does not make sense to you (I am assuming that several people will read
# this) please hop on IRC and let's chat.
#
# As an additional note, rvm has two verbosity modes
#
# $ rvm use X # This will output 'now using X' and is
# # and is not necessairly meant for scripting.
#
# $ rvm X # This will use X but not output anything
# # and is meant for use with scripting.
#
 
source $HOME/.bashrc # if rvm is sourced there.
# alternatively you can: source $HOME/.rvm/scripts/rvm
 
# Select 1.8.7
rvm use 1.8.7
 
# Output the current ruby version
ruby -v
 
# Display the information about the current environment, within this script.
rvm info
 
# Display the current rubygems directory for environment, within the script.
rvm gemdir
 
# List installed rubies
rvm list

Hi @wayneeqeguin, question about using rvm in scripts. I am building some simple scripts to setup a stack (I know you have a tool for this and hope to get to the point where I can use yours soon). I am using rvm because the stack runs several things in several different rubies. In getting each ruby environment setup I need to do things like script the installation of some gems. Those commands are done in sub-shells of the script being run, and always pickup my default Ruby I think, instead of the ruby I tell it to use on the line just prior.

I have read about how to use rvm in scripts, and am sourcing rvm properly I think. I am just not sure why rvm use 1.8.7 becomes 2.0.0 on the next line when I do a gem install in a sub shell.

I am using bsfl (my own heavily modified fork) to run the commands and get nice feedback printed back to screen on success/fail. I don't think it is doing any magic that would make it different from just running

$(gem install htmlentities)

in a script.

This is the underlying function that is running the gem install commands:

cmd () {
    COMMAND="$@"

    msg "RUN -> $COMMAND"

    RESULT=$("$@" 2>&1)
    ERROR=$?

    check_status " ${COMMAND}" "${ERROR}"

    die_if_false $ERROR "$RESULT"

    return $ERROR
}

OK I tried your example and modified it a bit, and understand why it doesn't work.

# First sourced my bsfl and bsfl_steps scripts, then:
source $HOME/.rvm/scripts/rvm

# Select 1.8.7
rvm use 1.8.7

# Output the current ruby version
try -d ruby -v # => 1.8.7

# try to switch rubies
try -cmd rvm use 2.0.0

# The ruby in this shell hasn't changed
ruby -v # => 1.8.7

try -cmd rvm use 2.0.0

# There is no way to get into the context where the Ruby is set to 2.0.0,
# because it was done in a subshell whose context is now gone.
ruby -v # => 1.8.7
try -cmd ruby -v # => 1.8.7
try -d ruby -v # => 1.8.7

# Now doing it in the same process:
rvm use 2.0.0

# Since we switched to ruby 2.0.0 in this context, the subshells will have it as well.
ruby -v # => 2.0.0
try -cmd echo `ruby -v` # => 2.0.0
try -d echo `ruby -v` # => 2.0.0

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.