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

ぐ巨炮叔叔 提交于 2019-12-07 04:01:09

问题


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.7
rvm list #  This shows the ruby used in parent shell's rvm.

回答1:


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

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




回答2:


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.




回答3:


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.




回答4:


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.




回答5:


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


来源:https://stackoverflow.com/questions/5352827/why-doesnt-rvm-work-in-a-bash-script-like-it-works-in-an-interactive-shell

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!