Why doesn't RVM work in a bash script like it

2019-04-28 19:10发布

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.

标签: bash rvm
5条回答
我想做一个坏孩纸
2楼-- · 2019-04-28 19:51

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.

查看更多
一夜七次
3楼-- · 2019-04-28 19:53

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

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

查看更多
女痞
4楼-- · 2019-04-28 20:00

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.

查看更多
Luminary・发光体
5楼-- · 2019-04-28 20:01

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.

查看更多
相关推荐>>
6楼-- · 2019-04-28 20:11

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
查看更多
登录 后发表回答