tanke25616429のアウトプット

IT技術の基本を勉強したことをアウトプットします。Linux、Kubernetes、クラウド中心です。百番煎じくらいだけど誰かの役に立てばそれはそれでうれしい。

bash関連の設定ファイル(.bashrcと.bash_profile)

bash関連の設定ファイルについて簡単に触れる。ログオン時や、新たなシェルを起動した際に読み込まれるbash関連の設定ファイルがいくつか存在する。代表的なものは~/.bashrcと~/.bash_profileである。~/は各ユーザのホームディレクトリなので、これらの設定ファイルは個人ごとに設定できる。設定ファイル、と言っているが実態はシェルスクリプトとなっており編集したい場合はbashの記法で行う。

~/.bashrcはシェルの起動時、~/.bash_profileはログオン時に読み込まれる(スクリプトなので、=実行される)。~/.bash_profileはデフォルトは以下のようになっているので、内部で~/.bashrcを呼び出していることがわかる。したがって、個別の設定を追加したいときは~/.bashrcに追加しておけばシェルの起動時とログオン時のいずれの場合も実行される。なお、. ~/.bashrcの先頭のピリオドはカレントシェルで実行することを示す。

[testuser2@nuc-centos8 ~]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

設定を追加し、動作を確認してみる。~/.bashrcに追加すると、bashを新たに立ち上げたときに追加したコマンドが動作していることが確認できる。

[testuser2@nuc-centos8 ~]$ vi .bashrc
[testuser2@nuc-centos8 ~]$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions

echo add to bashrc
[testuser2@nuc-centos8 ~]$ bash
add to bashrc

一方で~/.bash_profileに設定を追加した場合は新たにbashを立ち上げても、追加したコマンドが実行されていないが改めてログインすると実行されることがわかる。

[testuser2@nuc-centos8 ~]$ vi .bash_profile
[testuser2@nuc-centos8 ~]$ cat .bash_profile
# .bash_profile

# Get the aliases and functions
if [ -f ~/.bashrc ]; then
        . ~/.bashrc
fi

# User specific environment and startup programs

echo add to bash_profile
[testuser2@nuc-centos8 ~]$ bash
add to bashrc
[testuser@nuc-centos8 ~]$ exit
ログアウト
[root@nuc-centos8 ~]# su - testuser2
add to bashrc
add to bash_profile

参考1:ピリオドをつけてシェルを実行する意味

~/.bash_profileの中に. ~/.bashrcと先頭にピリオドをつけて実行する構文があった。ピリオドの有無での動作の違いを参考までに確認する。

事前準備で変数fooを設定するシェルスクリプトを作成する。

[testuser2@nuc-centos8 ~]$ cat > foo.sh
#!/bin/bash
foo=123
echo $foo
[testuser2@nuc-centos8 ~]$ chmod +x foo.sh

ピリオドなしで実行する。シェルスクリプト内で設定したはずの変数fooが実行元のシェルには引き継がれていない。

[testuser2@nuc-centos8 ~]$ ./foo.sh
123
[testuser2@nuc-centos8 ~]$ echo $foo

ピリオドをつけて実行する。シェルスクリプト内で設定した変数fooが実行元のシェルでも設定されている。ピリオドをつけることによってfoo=123が、実行元のシェル=カレントシェルの上で実行されていることを示している。

[testuser2@nuc-centos8 ~]$ . ./foo.sh
123
[testuser2@nuc-centos8 ~]$ echo $foo
123

参考2:追加した設定をすぐに反映するには

追加した設定をすぐに反映させたい場合は、sourceコマンドを用いる。

[testuser2@nuc-centos8 ~]$ vi .bashrc
[testuser2@nuc-centos8 ~]$ cat .bashrc
# .bashrc

# Source global definitions
if [ -f /etc/bashrc ]; then
        . /etc/bashrc
fi

# User specific environment
PATH="$HOME/.local/bin:$HOME/bin:$PATH"
export PATH

# Uncomment the following line if you don't like systemctl's auto-paging feature:
# export SYSTEMD_PAGER=

# User specific aliases and functions
echo add to bashrc
alias youbi='date +%a' #新たに追加
[testuser2@nuc-centos8 ~]$ youbi
-bash: youbi: コマンドが見つかりません
[testuser2@nuc-centos8 ~]$ source .bashrc
add to bashrc
[testuser2@nuc-centos8 ~]$ youbi
火

参考にしたもの

3. 環境のカスタマイズ

www.koikikukan.com

.bashrcや.bash_profileなどの変更設定をすぐに反映させたい - ITmedia エンタープライズ