0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

設定ファイルの読み込み有無を分かりやすく確認

Posted at

一般ユーザでnodenvをインストールしてパスを通した後
ちょっとrootで用があってroot→一般に戻った時

[ec2-user@ip-xxx-xxx-xxx-xxx ~]$ sudo su 
[root@ip-xxx-xxx-xxx-xxx ec2-user]# su shindev

[shindev@ip-xxx-xxx-xxx-xxx ec2-user]# cd
[shindev@ip-xxx-xxx-xxx-xxx ~]# git clone nodenv〜
[shindev@ip-xxx-xxx-xxx-xxx ~]# vi ~/.bash_profile
# PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/.nodenv/bin
# export PATH
# eval "$(nodenv init -)"
# これを追記してnodenvのパスを通した
[shindev@ip-xxx-xxx-xxx-xxx ~]# source ~/.bash_profile
[shindev@ip-xxx-xxx-xxx-xxx ~]# nodenv -v
nodenv 1.4.0+3.631d0b6 #パス通った
[shindev@ip-xxx-xxx-xxx-xxx ~]# exit

[root@ip-xxx-xxx-xxx-xxx ec2-user]# 
# ちょっと用を済ませる
[root@ip-xxx-xxx-xxx-xxx ec2-user]# su shindev
# もう1回一般ユーザにスイッチ
[shindev@ip-xxx-xxx-xxx-xxx ec2-user]# 
[shindev@ip-xxx-xxx-xxx-xxx ~]# nodenv -v
bash: nodenv: コマンドが見つかりません #あら??

bash_profileにバスを通すコマンド入れてるはずなのに読み込まれてない〜
そう言えばbashrcとbash_profileってどういう条件で読まれるのかよく分かってないので
真面目に調べてみる。

確認方法

なんのこっちゃないけど各ファイルにechoコマンドを追記。

bashrc
# .bashrc
echo "~/.bashrc loaded" #これを追記
# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi
bash_profile
# .bash_profile
echo "~/.bash_profile loaded" #これを追記
# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin:$HOME/.nodenv/bin
export PATH
eval "$(nodenv init -)"

これをやっといて一旦rootになってsuコマンドで切り替えてみる。

[root@ip-XXX-XXX-XXX-XXX ~]# su shindev
~/.bashrc loaded #bashrcだけ読まれた
[shindev@ip-XXX-XXX-XXX-XXX root]$ 

あれ、じゃprofileは??

[root@ip-XXX-XXX-XXX-XXX ~]# su - shindev #ハイフン付きスイッチ
最終ログイン: 2020/10/11 () 17:12:21 JST日時 pts/0
~/.bash_profile loaded #読まれた!
~/.bashrc loaded
[shindev@ip-XXX-XXX-XXX-XXX ~]$ 

ということでした。
suコマンドのハイフンありなしは切り替え後のカレントパスが
そのままか切り替えユーザのホームディレクトリに移動するくらいの認識だったけど、
ちゃんと考えてやらないといけないレベルの話だった。

ハイフン無しはインパーソネートなノリなので
いつもの作業的には基本ハイフンありでやるべきだなと。

おわり。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?