web-dev-qa-db-ja.com

「/ bin / bash --login」を実行する必要があるのはなぜですか

Ruby 2.1.1をrvm経由でインストールして、新しいUbuntu 13.10サーバーをセットアップしました。

問題は、ユーザー「Rails」(RubyおよびRailsをインストールしたユーザー)に切り替えるたびに、UbuntuがRubyを認識する前に/bin/bash --loginを実行する必要があることです。 Railsまたはrvmがインストールされています。

誰かが知っていることを願っています:

  1. 上記のコマンドは何をしますか?
  2. なぜそれを実行する必要があるのですか?
  3. そして、それを一度だけ解決するために何ができますか? :)

どんな助けも大歓迎です!

11

システムがインストールされた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にある場合、それらはシェル環境に追加されます

  • su -l Railsまたはsu --login Railsまたは短縮形su - Railsを使用してログインシェルを明示的に呼び出す
  • ユーザーRailsがSSH経由でログインするとき
  • ログイン後にサブシェルをbash --loginとして起動する

ユーザーRailsに切り替える方法に関係なくRuby環境を設定する場合は、代わりに関連する変数定義をユーザーの~/.bashrcに移動できます。

11
steeldriver

私はこの質問が2年前に尋ねられたことを知っていますが、誰か(私のような)がまだそれに直面している場合:@steeldriverが正しい-あなたはそれらの3つのファイルの1つにあるbashrcに何かが欠けています。私の場合、この行を私の~/.bashrcに追加する必要がありました。

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