开发者

Why doesn't RVM work in a bash script like it works in an interactive shell?

If you type the command in console, it works.

But if you put them in a bash script, problem comes.

#!/bin/bash
rvm use 1.8开发者_JAVA百科.7
rvm list #  This shows the ruby used in parent shell's rvm.


The shell functions installed by RVM aren't necessarily exported to subshells. In your shell script, you can re-initialize RVM with something like the same command line that's in your .bash_profile or .bashrc:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"

After you do that, the rvm functions will be available within the shell script.


In the script you are most likely not loading rvm as a function.

http://rvm.io/rvm/basics/


Try to run echo -n START; rvm use 1.8.7 ; rvm list; echo -n END without the eval, and see what it outputs. Everything between START and END will be executed as a command.


From the rvm documentation, it sets up the current shell. When you execute a shell script, you are executing a new shell process. That new process dies when the script ends, so the updates made by rvm to your environment disappear.

To script this, you'll need to do something more like this:

#!/bin/bash
rvm use 1.8.7
exec /bin/bash

This will (hopefully) drop you into a shell process with the updated environment provided by rvm, and when you are finished, you can just exit from it to go back to your default environment.

Alternatively, you count set up an alias that will modify your current shell environment:

alias r187="rvm use 1.8.7"

Add that line to your .bashrc or .profile to have it stick around permanently. Any time you want to use 1.8.7, type r187. You can set up other ones for different versions too, if you like.


You do not need the eval, and if you still want it, you are using it incorrectly. I would recommend this:

#!/bin/bash
rvm use 1.8.7
rvm list

But if you really want the eval, go with this:

#!/bin/bash
eval rvm use 1.8.7; rvm list
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜