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


12

我刚刚安装了一个新的Ubuntu 13.10服务器,其中通过安装了ruby 2.1.1 rvm

问题是,每当我切换到用户“ rails”(安装了ruby和rails的用户)时,我都必须先运行,/bin/bash --login然后Ubuntu才能识别出ruby,rails或rvm已安装。

希望有人知道:

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

任何帮助表示赞赏!


2
您如何切换到用户“ rails”(普通su?或su -lor su --?)。用户“ rails”的登录外壳是什么?您是否修改了用户的PATH变量,以及是否修改了哪个文件(~/.profile~/.bashrc~/.bash_profile或...?)
steeldriver 2014年

我现在看到的问题仅在我使用su命令从root切换到rails用户时才会发生。当我与Rails用户一起使用ssh时,不会发生此问题。但是我仍然想知道/ bin / bash --login的作用。:)
Ole HenrikSkogstrøm2014年

Answers:


11

听起来好像系统在只为登录shell读取的文件中指定了找到已安装的ruby组件所需的环境。在bash手册页上,这说了登录外壳程序和非登录外壳程序之间的区别:

INVOCATION
   A  login shell is one whose first character of argument zero is a -, or
   one started with the --login option.

   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.  

   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. 

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

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

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


嗯,好的,我想我需要最后一部分的例子。我是linux和ubuntu的新手。有哪些相关的变量定义?他们现在写在哪个文件中?你能给我一个例子吗?这有点理论上。
Ole HenrikSkogstrøm2014年

5

我知道这个问题是2年前提出的,但是如果有人(像我这样)仍然面对它:@steeldriver是正确的-您丢失了bashrc这三个文件之一中确实包含的某些内容。就我而言,我只需要将此行添加到我的行中~/.bashrc

[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.