2.1. 環境変数を設定する/setting environment variables

プログラムの動作をカスタマイズするために、パラメタを渡す手段の一つに 環境変数(environment variable)があります。演習科目の中で何度か、環境変数に 値を設定する必要が生じます。このチュートリアルではその設定の仕方を紹介します。

設定の仕方にはログアウトするまでの間で一時的に指定する方法と、 ファイルに保存しておいてログインの度に設定値が読み込まれるように する方法とがあります。その両方を説明しますので、実際に設定しておいてください。

Environment variables is one way to give parameters to programs in order to customize their behavior. In the seminar, there will be several occasions that environment variables must be set. In this tutorial we will show how to do that.

Environment variables can be set either temporary—i.e. set the value only until you logout, or permanently—i.e. store the value in a file so that the value will be used every time you log in. Both ways will be explained. Follow the instructions to actually do the setup.

2.1.1. 環境変数とは何か/What are environment variables

環境変数とは、OSが プロセス を起動する際に、親プロセスから子プロセスに 引き渡される文字列専用の変数です。

Environment variables are string-only variables that are passed from the parent process to the child process upon process creation by the operating system.

全てのプロセスは、起動時に親プロセスから環境変数一式のコピーを受け継ぎます。 一般的なプログラムでは環境変数の内容は読み出すだけで変更はしません。 仮に変更した場合、プロセスに与えられたコピーを変更することになるので、 親プロセスの環境変数は変わりません。 変更した値は、更に子プロセスを起動した場合に、子プロセスに引き継がれます。

All newly born processes are given a copy of all the environment variables that were held by the parent process. Most programs will only read the values and will not modify. If a program modified an environment variable, it will be modifying the copy that was given to the process, so the value in the parent process will not be affected. If the process created child processes, the modified values will be passed to those child processes.

2.1.2. 環境変数の設定手段/The means to set environemnt variables

環境変数を設定する一般的な手段は、 シェル の組み込み コマンドで設定する方法です。OSによっては、これに加えてOS特有の GUIによって環境変数が設定できるようになっていることもあります (例:Windowsのコントロールパネル)。

The standard means to set environment variables is to use built-in commands of the shell. Depending on the brand of OS, an additional GUI to set environment variables may be provided, for example, the control panel of Microsoft Windows.

2.1.3. 環境変数の内容を確認する/Check the value of environment variables

シェルの組み込みコマンド echo を使うと、環境変数の内容を確認できます。 変数の値を参照するには、変数名の前に変数参照演算子 “$” をつけます。

You can check the content of an environment variable with the builtin command ‘echo’. To obtain the content of a variable, use the variable reference operator ‘$’:

$ echo $SHELL
/usr/bin/bash
$ echo $USER
hideo-t

定義されている全ての環境変数を表示するには env コマンドを使います。

To display all environment variables, use the env command:

$ env
XDG_SEAT_PATH=/org/freedesktop/DisplayManager/Seat0
XDG_CONFIG_DIRS=/etc/xdg/xdg-xfce:/etc/xdg:/etc/xdg
LANG=ja_JP.UTF-8
DISPLAY=:0.0
SHLVL=1
...

2.1.4. 一時的に環境変数を設定する/Setting environment variables temporarily

シェルの変数には(環境変数ではない)シェル変数と環境変数があります。 shellの種類によってはシェル変数への代入と環境変数への代入に、全く 異なる構文を使うものもありますが、bash,sh,zshなどの現在広く使われている シェルでは、部分的に共通の構文を使います。 シェル変数に一旦代入してから、環境変数としてエクスポートする、 という手順で環境変数に設定します。

Variables in shells come in two flavors, the (non-environment) shell variables, and environment variables. In some types of shell implementations the assignment to the two different variables take separate forms, but for the currently popular shells such as bash, sh, zsh, the two types of assignments take the same form up to a point. To assign a value to an environment variable, you first assign a value to a shell variable, then ‘export’ it to copy the value to an environemnt variable with the same name:

$ VAR_A="This is a shell var."
$ VAR_B="This is an env var."
$ export VAR_B
$ echo $VAR_A
This is a shell var.
$ echo $VAR_B
This is an env var.
$ env | grep VAR_
VAR_B=This is an env var.
$ bash
$ echo $VAR_A

$ echo $VAR_B
This is an env var.
$ exit
echo $VAR_A
This is a shell var.

上の例では途中でbashプログラムを子プロセスとして起動しています。 親のシェルから子プロセスのシェルには、シェル変数は引き継がれないので、 親プロセスのシェル変数 VAR_A の内容は、子プロセスでは未定義になっています。

In the above example, a bash program is invoked as a child process. Shell variables in the parent shell is not inherited to the latter shell running as the child process, therefore VAR_A, which is a shell variable of the parent process is undefined within the child process.

上記のように設定された環境変数はシェルのメモリの中にしか存在しないので、 シェルを終了すると消えてしまいます。 設定をファイルに保存して、消えないようにする方法を次に紹介します。

Environment variables that were set in the above way exists only in memory, and will be lost when you exit from the shell. We will next see how the setting can be stored into a file so that it won’t be lost.

2.1.5. シェルの初期化ファイルで環境変数を設定する方法/Setting environment variables in shell startup files

bashの場合、ホームディレクトリに .profile というファイルがあれば、 最初に起動するシェルがそのファイルを読み込みます。 そこに環境変数への代入を書いておけば、ログインの度に同じ値が設定される ようになります。

In the case of bash, if there is a file named .profile in your home directory, the first shell in your login session will read that file. By placing environment variable assignments in that file, the settings will be read every time you log in.

環境変数への設定がしばしば必要になる例として、環境変数 PATH に ディレクトリ名を追加する例を示します。 .profileを編集するには テキストエディタ を使います。

As an example of a common cause for setting environment variables, adding a directory to the directory list that is held by the environment variable PATH can be done as follows. Use a Text Editor to edit the .profile file.:

MY_BIN_PATH=~/.local/bin
INSTRUCTOR_BIN_PATH=~/bin
PATH=$PATH:$MY_BIN_PATH:$INSTRUCTOR_BIN_PATH
export PATH

上記の例では、自分のホームディレクトリの下にある .local/bin ディレクトリと、演習科目の講師のホームディレクトリの下にある pub/binディレクトリがPATHに追加されます。

The above example will add two directories to your path; a .local/bin directory under your home directory, and a pub/bin directory under the instructor’s home directory.

.profileにPATHの設定を追加した直後は、まだファイルがシェルに 読み込まれていないので設定内容が反映されないことに注意してください。 一度ログアウトしてログインしなおすと、反映されます。

このファイルの書き方は、当冪でないことに注意してください。 環境変数PATHには元から値が設定されており、それに追加するように 書いています。何らかの理由で.profileを複数回実行すると、 追加処理が何度も行われてしまいます。それを防ぐ方法もありますので 興味の湧いた方は、調べてみてください。

Note that the PATH assignment that has been added to your .profile file will not be reflected immediately to an already running shell. The assignment will take effect once you log out and log in again.

Also note that the way the file is written is not idempotent. The environment variable PATH will already have some value set, and the above code will append a string to the existing value. If, for some reason, .profile is read and executed multiple times, the appending operation will be run multiple times. There are ways to avoid that. If you are interested, try some searching.