LoginSignup
11
8

More than 1 year has passed since last update.

Ubuntuで環境変数を設定するには

Last updated at Posted at 2019-11-20

環境

Ubuntu 16.04.5 LTS

はじめに

環境変数を設定する場合、ググると、~/.bash_profile に記述するように書かれている記事が多いが、自分の環境では、デフォルトで、~/.bash_profileが存在していない。この場合、どうすればいいのだろうか。

方法

~/.bash_profileは存在していないが、ホームディレクトリによく似たファイルで、~/.profileが存在している。この中身を見るとヒントがあった。

~/.profile: executed by the command interpreter for login shells.
This file is not read by bash(1), if ~/.bash_profile or ~/.bash_login exists.
see /usr/share/doc/bash/examples/startup-files for examples.
the files are located in the bash-doc package.

~/.profileは、ログインシェルから実行される。
~/.bash_profile または、~/.bash_loginが存在すれば、~/.profileは、実行されまれません。

ここを見て解決した!
https://askubuntu.com/questions/510709/i-cannot-find-bash-profile-in-ubuntu

つまり、ログインシェルは、/etc/profileを読み込んだ後、下記の順番で読み込もうとするようだ。ポイントは、~/.bash_profileが存在していれば、以下のファイルは読み込まれなくなるという事。

  1. ~/.bash_profile
  2. ~/.bash_login
  3. ~/.profile

~/.bash_profileがないからと言って、新規で、~/.bash_profileを作ってしまうと、デフォルトで存在していた、~/.profileが読み込まれなくなるという事になる。~/.profileの中を見ると分かるのだが、~/.bashrcは、~/.profileから実行されています。

~/.profileを~/.bash_profileにリネームして、~/.bash_profileに環境変数を追加してもいいのだろうが、~/.profileが存在していれば、環境変数は、素直に、~/.profileに記述しましょうというお話でした。

補足

シェルスクリプトの起動順番については、ここの教材でも学習できるよ!
https://www.techpit.jp/courses/190

11
8
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
11
8