迺ー蠅�、画焚繧定ィュ螳壹☆繧�/setting environment variables ================================================ 繝励Ο繧ー繝ゥ繝�縺ョ蜍穂ス懊r繧ォ繧ケ繧ソ繝槭う繧コ縺吶k縺溘a縺ォ縲√ヱ繝ゥ繝。繧ソ繧呈ク。縺呎焔谿オ縺ョ荳縺、縺ォ 迺ー蠅�、画焚(environment variable)縺後≠繧翫∪縺吶よシ皮ソ堤ァ醍岼縺ョ荳ュ縺ァ菴募コヲ縺九∫腸蠅�、画焚縺ォ 蛟、繧定ィュ螳壹☆繧句ソ�ヲ√′逕溘§縺セ縺吶ゅ%縺ョ繝√Η繝シ繝医Μ繧「繝ォ縺ァ縺ッ縺昴�險ュ螳壹�莉墓婿繧堤エケ莉九@縺セ縺吶� 險ュ螳壹�莉墓婿縺ォ縺ッ繝ュ繧ー繧「繧ヲ繝医☆繧九∪縺ァ縺ョ髢薙〒荳譎ら噪縺ォ謖�ョ壹☆繧区婿豕輔→縲� 繝輔ぃ繧、繝ォ縺ォ菫晏ュ倥@縺ヲ縺翫>縺ヲ繝ュ繧ー繧、繝ウ縺ョ蠎ヲ縺ォ險ュ螳壼、縺瑚ェュ縺ソ霎シ縺セ繧後k繧医≧縺ォ 縺吶k譁ケ豕輔→縺後≠繧翫∪縺吶ゅ◎縺ョ荳。譁ケ繧定ェャ譏弱@縺セ縺吶�縺ァ縲∝ョ滄圀縺ォ險ュ螳壹@縺ヲ縺翫>縺ヲ縺上□縺輔>縲� 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. 迺ー蠅�、画焚縺ィ縺ッ菴輔°/What are environment variables ----------------------------------------------- 迺ー蠅�、画焚縺ィ縺ッ縲^S縺� :term:`繝励Ο繧サ繧ケ` 繧定オキ蜍輔☆繧矩圀縺ォ縲∬ヲェ繝励Ο繧サ繧ケ縺九i蟄舌�繝ュ繧サ繧ケ縺ォ 蠑輔″貂。縺輔l繧区枚蟄怜�蟆ら畑縺ョ螟画焚縺ァ縺吶� Environment variables are string-only variables that are passed from the parent process to the child process upon :term:`process` creation by the operating system. 蜈ィ縺ヲ縺ョ繝励Ο繧サ繧ケ縺ッ縲∬オキ蜍墓凾縺ォ隕ェ繝励Ο繧サ繧ケ縺九i迺ー蠅�、画焚荳蠑上�繧ウ繝斐�繧貞女縺醍カ吶℃縺セ縺吶� 荳闊ャ逧�↑繝励Ο繧ー繝ゥ繝�縺ァ縺ッ迺ー蠅�、画焚縺ョ蜀�ョケ縺ッ隱ュ縺ソ蜃コ縺吶□縺代〒螟画峩縺ッ縺励∪縺帙s縲� 莉ョ縺ォ螟画峩縺励◆蝣エ蜷医√�繝ュ繧サ繧ケ縺ォ荳弱∴繧峨l縺溘さ繝斐�繧貞、画峩縺吶k縺薙→縺ォ縺ェ繧九�縺ァ縲� 隕ェ繝励Ο繧サ繧ケ縺ョ迺ー蠅�、画焚縺ッ螟峨o繧翫∪縺帙s縲� 螟画峩縺励◆蛟、縺ッ縲∵峩縺ォ蟄舌�繝ュ繧サ繧ケ繧定オキ蜍輔@縺溷�エ蜷医↓縲∝ュ舌�繝ュ繧サ繧ケ縺ォ蠑輔″邯吶′繧後∪縺吶� 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. 迺ー蠅�、画焚縺ョ險ュ螳壽焔谿オ/The means to set environemnt variables --------------------------------------------------------- 迺ー蠅�、画焚繧定ィュ螳壹☆繧倶ク闊ャ逧�↑謇区ョオ縺ッ縲� :term:`繧キ繧ァ繝ォ` 縺ョ邨�∩霎シ縺ソ 繧ウ繝槭Φ繝峨〒險ュ螳壹☆繧区婿豕輔〒縺吶0S縺ォ繧医▲縺ヲ縺ッ縲√%繧後↓蜉�縺医※OS迚ケ譛峨� GUI縺ォ繧医▲縺ヲ迺ー蠅�、画焚縺瑚ィュ螳壹〒縺阪k繧医≧縺ォ縺ェ縺」縺ヲ縺�k縺薙→繧ゅ≠繧翫∪縺� (萓具シ啗indows縺ョ繧ウ繝ウ繝医Ο繝シ繝ォ繝代ロ繝ォ)縲� The standard means to set environment variables is to use built-in commands of the :term:`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. 迺ー蠅�、画焚縺ョ蜀�ョケ繧堤「コ隱阪☆繧�/Check the value of environment variables ----------------------------------------------------------------- 繧キ繧ァ繝ォ縺ョ邨�∩霎シ縺ソ繧ウ繝槭Φ繝� echo 繧剃スソ縺�→縲∫腸蠅�、画焚縺ョ蜀�ョケ繧堤「コ隱阪〒縺阪∪縺吶� 螟画焚縺ョ蛟、繧貞盾辣ァ縺吶k縺ォ縺ッ縲∝、画焚蜷阪�蜑阪↓螟画焚蜿ら�貍皮ョ怜ュ� "$" 繧偵▽縺代∪縺吶� 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 螳夂セゥ縺輔l縺ヲ縺�k蜈ィ縺ヲ縺ョ迺ー蠅�、画焚繧定。ィ遉コ縺吶k縺ォ縺ッ env 繧ウ繝槭Φ繝峨r菴ソ縺�∪縺吶� 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 ... 荳譎ら噪縺ォ迺ー蠅�、画焚繧定ィュ螳壹☆繧�/Setting environment variables temporarily -------------------------------------------------------------------- 繧キ繧ァ繝ォ縺ョ螟画焚縺ォ縺ッ�育腸蠅�、画焚縺ァ縺ッ縺ェ縺�シ峨す繧ァ繝ォ螟画焚縺ィ迺ー蠅�、画焚縺後≠繧翫∪縺吶� shell縺ョ遞ョ鬘槭↓繧医▲縺ヲ縺ッ繧キ繧ァ繝ォ螟画焚縺ク縺ョ莉」蜈・縺ィ迺ー蠅�、画焚縺ク縺ョ莉」蜈・縺ォ縲∝�縺� 逡ー縺ェ繧区ァ区枚繧剃スソ縺�b縺ョ繧ゅ≠繧翫∪縺吶′縲|ash,sh,zsh縺ェ縺ゥ縺ョ迴セ蝨ィ蠎�¥菴ソ繧上l縺ヲ縺�k 繧キ繧ァ繝ォ縺ァ縺ッ縲�Κ蛻�噪縺ォ蜈ア騾壹�讒区枚繧剃スソ縺�∪縺吶� 繧キ繧ァ繝ォ螟画焚縺ォ荳譌ヲ莉」蜈・縺励※縺九i縲∫腸蠅�、画焚縺ィ縺励※繧ィ繧ッ繧ケ繝昴�繝医☆繧九� 縺ィ縺�≧謇矩��〒迺ー蠅�、画焚縺ォ險ュ螳壹@縺セ縺吶� 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繝励Ο繧ー繝ゥ繝�繧貞ュ舌�繝ュ繧サ繧ケ縺ィ縺励※襍キ蜍輔@縺ヲ縺�∪縺吶� 隕ェ縺ョ繧キ繧ァ繝ォ縺九i蟄舌�繝ュ繧サ繧ケ縺ョ繧キ繧ァ繝ォ縺ォ縺ッ縲√す繧ァ繝ォ螟画焚縺ッ蠑輔″邯吶′繧後↑縺��縺ァ縲� 隕ェ繝励Ο繧サ繧ケ縺ョ繧キ繧ァ繝ォ螟画焚 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. 荳願ィ倥�繧医≧縺ォ險ュ螳壹&繧後◆迺ー蠅�、画焚縺ッ繧キ繧ァ繝ォ縺ョ繝。繝「繝ェ縺ョ荳ュ縺ォ縺励°蟄伜惠縺励↑縺��縺ァ縲� 繧キ繧ァ繝ォ繧堤オゆコ�☆繧九→豸医∴縺ヲ縺励∪縺�∪縺吶� 險ュ螳壹r繝輔ぃ繧、繝ォ縺ォ菫晏ュ倥@縺ヲ縲∵カ医∴縺ェ縺�h縺�↓縺吶k譁ケ豕輔r谺。縺ォ邏ケ莉九@縺セ縺吶� 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. 繧キ繧ァ繝ォ縺ョ蛻晄悄蛹悶ヵ繧。繧、繝ォ縺ァ迺ー蠅�、画焚繧定ィュ螳壹☆繧区婿豕�/Setting environment variables in shell startup files --------------------------------------------------------------------------------------------------- bash縺ョ蝣エ蜷医√�繝シ繝�繝�ぅ繝ャ繧ッ繝医Μ縺ォ .profile 縺ィ縺�≧繝輔ぃ繧、繝ォ縺後≠繧後�縲� 譛蛻昴↓襍キ蜍輔☆繧九す繧ァ繝ォ縺後◎縺ョ繝輔ぃ繧、繝ォ繧定ェュ縺ソ霎シ縺ソ縺セ縺吶� 縺昴%縺ォ迺ー蠅�、画焚縺ク縺ョ莉」蜈・繧呈嶌縺�※縺翫¢縺ー縲√Ο繧ー繧、繝ウ縺ョ蠎ヲ縺ォ蜷後§蛟、縺瑚ィュ螳壹&繧後k 繧医≧縺ォ縺ェ繧翫∪縺吶� 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. 迺ー蠅�、画焚縺ク縺ョ險ュ螳壹′縺励�縺励�蠢�ヲ√↓縺ェ繧倶セ九→縺励※縲∫腸蠅�、画焚 :term:`PATH` 縺ォ 繝�ぅ繝ャ繧ッ繝医Μ蜷阪r霑ス蜉�縺吶k萓九r遉コ縺励∪縺吶� .profile繧堤キィ髮�☆繧九↓縺ッ :ref:`繝�く繧ケ繝医お繝�ぅ繧ソ<text-editor>` 繧剃スソ縺�∪縺吶� 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 :term:`PATH` can be done as follows. Use a :ref:`Text Editor<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 荳願ィ倥�萓九〒縺ッ縲∬�蛻��繝帙�繝�繝�ぅ繝ャ繧ッ繝医Μ縺ョ荳九↓縺ゅk .local/bin 繝�ぅ繝ャ繧ッ繝医Μ縺ィ縲∵シ皮ソ堤ァ醍岼縺ョ隰帛クォ縺ョ繝帙�繝�繝�ぅ繝ャ繧ッ繝医Μ縺ョ荳九↓縺ゅk pub/bin繝�ぅ繝ャ繧ッ繝医Μ縺訓ATH縺ォ霑ス蜉�縺輔l縺セ縺吶� 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縺ョ險ュ螳壹r霑ス蜉�縺励◆逶エ蠕後�縲√∪縺�繝輔ぃ繧、繝ォ縺後す繧ァ繝ォ縺ォ 隱ュ縺ソ霎シ縺セ繧後※縺�↑縺��縺ァ險ュ螳壼�螳ケ縺悟渚譏�縺輔l縺ェ縺�%縺ィ縺ォ豕ィ諢上@縺ヲ縺上□縺輔>縲� 荳蠎ヲ繝ュ繧ー繧「繧ヲ繝医@縺ヲ繝ュ繧ー繧、繝ウ縺励↑縺翫☆縺ィ縲∝渚譏�縺輔l縺セ縺吶� 縺薙�繝輔ぃ繧、繝ォ縺ョ譖ク縺肴婿縺ッ縲∝ス灘�縺ァ縺ェ縺�%縺ィ縺ォ豕ィ諢上@縺ヲ縺上□縺輔>縲� 迺ー蠅�、画焚PATH縺ォ縺ッ蜈�°繧牙、縺瑚ィュ螳壹&繧後※縺翫j縲√◎繧後↓霑ス蜉�縺吶k繧医≧縺ォ 譖ク縺�※縺�∪縺吶ゆス輔i縺九�逅�罰縺ァ.profile繧定、�焚蝗槫ョ溯。後☆繧九→縲� 霑ス蜉�蜃ヲ逅�′菴募コヲ繧り。後o繧後※縺励∪縺�∪縺吶ゅ◎繧後r髦イ縺先婿豕輔b縺ゅj縺セ縺吶�縺ァ 闊亥袖縺ョ貉ァ縺�◆譁ケ縺ッ縲∬ェソ縺ケ縺ヲ縺ソ縺ヲ縺上□縺輔>縲� 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.