Why does “rvm use” command require/suggest a login shell?

随声附和 提交于 2020-04-30 06:55:46

问题


With a default installation of RVM and from a non-login shell, executing rvm use produces:

RVM is not a function, selecting rubies with 'rvm use ...' will not work.

You need to change your terminal emulator preferences to allow login shell.
Sometimes it is required to use `/bin/bash --login` as the command.
Please visit https://rvm.io/integration/gnome-terminal/ for an example.

Where the reason for this warning is that the following sourcing line is added only in files like ~/.bash_profile during installation:

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm" # Load RVM into a shell session *as a function*

And the previous can even be confirmed as expected behavior from https://rvm.io/support/faq#shell_login:

RVM by default adds itself currently to ~/.bash_profile file, and the recommended way is to enable login shell in gnome-terminal (and screen).

But, why isn't the RVM installer simply adding the previous line in files like ~/.bashrc so commands like rvm use work both for login and non-login shells?.

Similar questions without a proper/official answer:

  • Why rvm needs login shell?
  • https://askubuntu.com/questions/444880/why-do-i-need-to-run-bin-bash-login

回答1:


The main reason here is that rvm must be defined as a function of your shell and not as a rvm-use script.

If defined as a script, rvm-use would operate in a separate subprocess and only had an access to a copy of your shell environment, not to the original env. Because rvm use needs to actually modify your local PATH environment (to prepend rvm ruby shims for correct version) it needs full access to your shell environment - hence use of function is required.

This means, you need to load this function somewhere - it is (most likely, didn't fully check it) done via /etc/profile, which loads /etc/profile.d/rvm.sh file. This file needs to be either manually sourced or is loaded automatically when terminal opens as a login shell.

Now, why does rvm needs to modify local terminal environment instead of using single global state? It is to allow us to have few terminals open with different ruby versions active at the same time.



来源:https://stackoverflow.com/questions/61277999/why-does-rvm-use-command-require-suggest-a-login-shell

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