为什么 RVM 不能像在交互式 shell 中那样在 bash 脚本中工作?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5352827/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 23:38:02  来源:igfitidea点击:

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

bashrvm

提问by Cheng

If you type the command in console, it works.

如果您在控制台中键入命令,它会起作用。

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

但是如果你把它们放在 bash 脚本中,问题就来了。

#!/bin/bash
rvm use 1.8.7
rvm list #  This shows the ruby used in parent shell's rvm.

回答by Brett

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:

RVM 安装的 shell 函数不一定导出到子 shell。在您的 shell 脚本中,您可以使用类似于 .bash_profile 或 .bashrc 中的相同命令行的内容重新初始化 RVM:

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

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

执行此操作后,rvm 函数将在 shell 脚本中可用。

回答by Wayne E. Seguin

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

在脚本中,您很可能不会将 rvm 作为函数加载。

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

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

回答by l0b0

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

尝试在echo -n START; rvm use 1.8.7 ; rvm list; echo -n END没有eval, 的情况下运行,看看它输出什么。START 和 END 之间的所有内容都将作为命令执行。

回答by Jonathan

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 rvmto your environment disappear.

rvm 文档中,它设置了当前的 shell。当您执行一个 shell 脚本时,您正在执行一个新的 shell 进程。该新进程在脚本结束时终止,因此rvm对您的环境所做的更新消失了。

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 exitfrom it to go back to your default environment.

这将(希望)让您进入具有由 提供的更新环境的 shell 进程rvm,当您完成时,您可以exit从它返回到您的默认环境。

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

或者,您可以设置一个别名来修改您当前的 shell 环境:

alias r187="rvm use 1.8.7"

Add that line to your .bashrcor .profileto 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.

将该行添加到您的.bashrc.profile永久保留。任何时候你想使用1.8.7,输入r187. 如果您愿意,您也可以为不同的版本设置其他版本。

回答by Jonathan

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

您不需要eval,如果您仍然需要它,那么您使用它是错误的。我会推荐这个:

#!/bin/bash
rvm use 1.8.7
rvm list

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

但是,如果您真的想要eval,请执行以下操作:

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