为什么我需要运行“/bin/bash --login”

Ole*_*røm 19 rvm ruby bash login 13.10

我刚刚设置了一个新的 Ubuntu 13.10 服务器,通过rvm.

问题是,每当我切换到用户“rails”(我安装了 ruby​​ 和 rails 的用户)时,我必须/bin/bash --login在 Ubuntu 识别出 ruby​​、rails 或rvm已安装之前运行。

希望有人知道:

  1. 上面的命令有什么作用?
  2. 为什么我需要运行它?
  3. 我能做些什么来一劳永逸地解决它?:)

任何帮助表示赞赏!

ste*_*ver 16

听起来系统查找已安装的 ruby​​ 组件所需的环境是在一个文件中指定的,该文件只能为登录 shell 读取。bash 手册页有关于登录 shell 和非登录 shell 之间的区别的说明:

INVOCATION
   A  login shell is one whose first character of argument zero is a -, or
   one started with the --login option.
Run Code Online (Sandbox Code Playgroud)

   When bash is invoked as an interactive login shell, or as a non-inter?
   active shell with the --login option, it first reads and executes  com?
   mands  from  the file /etc/profile, if that file exists. After reading
   that file, it looks for ~/.bash_profile, ~/.bash_login, and ~/.profile,
   in  that order, and reads and executes commands from the first one that
   exists and is readable.  
Run Code Online (Sandbox Code Playgroud)

然而

   When an interactive shell that is not a login shell  is  started,  bash
   reads  and  executes  commands  from /etc/bash.bashrc and ~/.bashrc, if
   these files exist. 
Run Code Online (Sandbox Code Playgroud)

因此,如果 ruby​​ 环境变量在/home/rails/.profile/etc/profile例如,它们将被添加到 shell 环境中

  • 通过使用su -l railssu --login rails或速记显式调用登录shellsu - rails
  • 当用户rails通过 SSH 登录时
  • 通过bash --login在登录后启动子shell

如果无论您如何切换到 user 都希望设置 ruby​​ 环境rails,您可以将相关的变量定义移动到用户的~/.bashrc


小智 7

我知道这个问题是在 2 年前提出的,但如果有人(像我一样)仍然面临这个问题:@steeldriver 是对的——你在bashrc这 3 个文件之一中遗漏了一些东西。在我的情况下,我只需要将此行添加到我的~/.bashrc

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
Run Code Online (Sandbox Code Playgroud)