# 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¶

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¶

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

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.

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.

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

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.