Linux bashの設定ファイル

Linuxのbash設定ファイルについてです。

目次

サンプル bash設定ファイルの記号の意味
  /etc/profile
~/.bash_profile
  ~/.bashrc
  ~/.bash_logout
ログイン時にhello worldの文字を表示するサンプル

/bash設定ファイルの記号の意味

  1. 「~/」 はユーザーのホームディレクトリを表します。
    ログインユーザがtestの場合のホームディレクトリは/home/testです。
  2. ファイルの前にある.は隠しファイルを表します。
    lsコマンドでaをつけると表示されます。

 

/etc/profile

ログイン時に、読み込まれます。全ユーザーに適用されます。

 

~/.bash_profile

ログイン時に読み込まれます。ログインユーザーのみに設定されます。

.bash_profileのコード

# .bash_profile

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

# User specific environment and startup programs

PATH=$PATH:$HOME/bin

export PATH

4行目から6行目に ~/.bashrcがあれば ~/.bashrcを読み込むと記述されています。

5行目のbashrcの読み込みをコメントにしてログインしたときの画面です。

5行目の.bashrcの読み込みをコメントにせずログインしたときの画面です。

 

~/.bashrc

.bash_profileの中に~/.bashrcを読み込む記述があるためログイン時に読み込まれます。

上記箇所をコメントにすると読み込まれません。

bashコマンドでbashを起動すると~/.bashrcが読み込まれます。

.bashrcのコード

# .bashrc

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

# User specific aliases and functions

4行目から6行目は、 /etc/bashrcがあれば/etc/bashrcを読み込みます。

 

~/.bash_logout

ログアウトする時に読み込まれます。

 

ログイン時にhello worldの文字を表示するサンプル

ログイン時に文字列を表示するようにします。

viで~/.bash_profileを開きます。

# .bash_profile

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

# User specific environment and startup programs

PATH=$PATH:$HOME/.local/bin:$HOME/bin

export PATH
echo "hello world"

13行目を追記しました。

ログインすると以下のように表示されます。

関連の記事

Linux サービスの自動起動の設定とランレベル
Linux bashの主な機能

△上に戻る