「環境変数」の版間の差分

提供: ArchWiki
ナビゲーションに移動 検索に移動
12行目: 12行目:
 
{{Note|Some environment variables are user-specific. Check by comparing the outputs of ''printenv'' as an unprivileged user and as ''root''.}}
 
{{Note|Some environment variables are user-specific. Check by comparing the outputs of ''printenv'' as an unprivileged user and as ''root''.}}
   
  +
{{ic|env}} ユーティリティを使うことで環境変数を変更してコマンドを実行することができます。以下の例では環境変数 {{ic|EDITOR}} を {{ic|vim}} に設定して ''xterm'' を起動します。以下のコマンドを実行してもグローバルな環境変数 {{ic|EDITOR}} に影響は与えません。
The {{ic|env}} utility can be used to run a command under a modified environment. The following example will launch ''xterm'' with the environment variable {{ic|EDITOR}} set to {{ic|vim}}. This will not affect the global environment variable {{ic|EDITOR}}.
 
   
 
$ env EDITOR=vim xterm
 
$ env EDITOR=vim xterm
   
The [[Bash]] builtin ''set'' allows you to change the values of shell options and set the positional parameters, or to display the names and values of shell variables. For more information, see the ''set'' documentation: [http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin].
+
[[Bash]] に組み込まれている ''set'' はシェルのオプションの値を変更したり、位置のパラメータを設定したり、シェル変数の名前や値を表示することができます。詳しくは、''set'' のドキュメントを見て下さい: [http://www.gnu.org/software/bash/manual/bash.html#The-Set-Builtin]
   
  +
特定のプロセスで使われている環境変数を確認したい場合は、{{ic|/proc/${PID}/environ}} を見て下さい。このファイルには特殊な {{ic|\x0}} 文字が含まれています。次の関数を使うことでファイルを解析できます。
To see what environment variables are available to a specific process, look in {{ic|/proc/${PID}/environ}}. This file contains special {{ic|\x0}} characters. You can parse it with this function.
 
   
 
# envof() { sed 's/\x0/\n/g' /proc/${1}/environ; }
 
# envof() { sed 's/\x0/\n/g' /proc/${1}/environ; }
25行目: 25行目:
   
 
== サンプル ==
 
== サンプル ==
  +
以下のセクションでは Linux システムで一般的に使われている環境変数を並べており、それぞれの値について説明しています。
The following section lists a number of common environment variables used by a Linux system and describes their values.
 
   
*{{ic|DE}} indicates the ''D''esktop ''E''nvironment being used. [[xdg-open]] will use it to choose more user-friendly file-opener application that desktop environment provides. Some packages need to be installed to use this feature. For [[GNOME]], that would be {{pkg|libgnome}}; for [[Xfce]] this is {{pkg|exo}}. Recognised values of {{ic|DE}} variable are: {{ic|gnome}}, {{ic|kde}}, {{ic|xfce}}, {{ic|lxde}} and {{ic|mate}}.
+
*{{ic|DE}} は使用しているデスクトップ環境 (''D''esktop ''E''nvironment) を示します。[[xdg-open]] はこの環境変数を使ってデスクトップ環境に含まれているユーザーフレンドリーなファイルオープナアプリケーションを選択します。この機能を使うにはインストールする必要があるパッケージが存在します。[[GNOME]] の場合、{{pkg|libgnome}} が必要です。[[Xfce]] の場合、{{pkg|exo}} が必要です。使用される {{ic|DE}} 変数の値: {{ic|gnome}}, {{ic|kde}}, {{ic|xfce}}, {{ic|lxde}}, {{ic|mate}}
   
  +
:{{ic|DE}} 環境変数はウィンドウマネージャを起動する前にエクスポートする必要があります。例:
:The {{ic|DE}} environment variable needs to be exported before starting the window manager. For example:
 
   
 
{{hc|~/.xinitrc|2=
 
{{hc|~/.xinitrc|2=
38行目: 38行目:
 
:This will make ''xdg-open'' use the more user-friendly ''exo-open'', because it assumes it is running inside Xfce. Use ''exo-preferred-applications'' for configuring.
 
:This will make ''xdg-open'' use the more user-friendly ''exo-open'', because it assumes it is running inside Xfce. Use ''exo-preferred-applications'' for configuring.
   
*{{ic|DESKTOP_SESSION}} is similar to {{ic|DE}}, but used in [[LXDE]] desktop enviroment: when {{ic|DESKTOP_SESSION}} is set to {{ic|LXDE}}, ''xdg-open'' will use ''pcmanfm'' file associations.
+
*{{ic|DESKTOP_SESSION}} {{ic|DE}} と似ていますが、[[LXDE]] デスクトップ環境で使われています: {{ic|DESKTOP_SESSION}} {{ic|LXDE}} に設定されている場合、''xdg-open'' ''pcmanfm'' のファイル関連付けを使います。
   
 
*{{ic|PATH}} contains a colon-separated list of directories in which your system looks for executable files. When a regular command (e.g., ''ls'', ''rc-update'' or ''ic|emerge'') is interpreted by the shell (e.g., ''bash'' or ''zsh''), the shell looks for an executable file with the same name as your command in the listed directories, and executes it. To run executables that are not listed in {{ic|PATH}}, the absoute path to the executable must be given: {{ic|/bin/ls}}.
 
*{{ic|PATH}} contains a colon-separated list of directories in which your system looks for executable files. When a regular command (e.g., ''ls'', ''rc-update'' or ''ic|emerge'') is interpreted by the shell (e.g., ''bash'' or ''zsh''), the shell looks for an executable file with the same name as your command in the listed directories, and executes it. To run executables that are not listed in {{ic|PATH}}, the absoute path to the executable must be given: {{ic|/bin/ls}}.
125行目: 125行目:
 
==== グラフィカルアプリケーション ====
 
==== グラフィカルアプリケーション ====
   
GUI アプリケーションの環境変数を設定したいときは、[[xinitrc]] に変数を記述することができます。例:
+
GUI アプリケーションの環境変数を設定したいときは、[[xinitrc]] ([[ディスプレイマネージャ]]を使う場合 [[xprofile]]) に変数を記述することができます。例:
   
 
{{hc|1=~/.xinitrc|2=
 
{{hc|1=~/.xinitrc|2=

2015年6月18日 (木) 17:05時点における版

環境変数とは1つ、または複数のアプリケーションによって使用されるデータを含んだ名前付きオブジェクトです。簡単に言えば、名前と値がある変数です。環境変数の値は例えば、実行可能ファイルのファイルシステムにおける場所や使用するデフォルトのエディタ、システムロケールの設定などになります。Linux に慣れてないユーザーは、環境変数で設定をするのは御しがたいと考えがちですが、複数のアプリケーションと Linux のプロセスの設定を共有するシンプルな方法として環境変数が存在しています。

ユーティリティ

coreutils パッケージには printenvenv というプログラムが入っています。現在設定されている環境変数とその値を表示するには:

$ printenv
ノート: Some environment variables are user-specific. Check by comparing the outputs of printenv as an unprivileged user and as root.

env ユーティリティを使うことで環境変数を変更してコマンドを実行することができます。以下の例では環境変数 EDITORvim に設定して xterm を起動します。以下のコマンドを実行してもグローバルな環境変数 EDITOR に影響は与えません。

$ env EDITOR=vim xterm

Bash に組み込まれている set はシェルのオプションの値を変更したり、位置のパラメータを設定したり、シェル変数の名前や値を表示することができます。詳しくは、set のドキュメントを見て下さい: [1]

特定のプロセスで使われている環境変数を確認したい場合は、/proc/${PID}/environ を見て下さい。このファイルには特殊な \x0 文字が含まれています。次の関数を使うことでファイルを解析できます。

# envof() { sed 's/\x0/\n/g' /proc/${1}/environ; }
# envof 1
TERM=linux

サンプル

以下のセクションでは Linux システムで一般的に使われている環境変数を並べており、それぞれの値について説明しています。

  • DE は使用しているデスクトップ環境 (Desktop Environment) を示します。xdg-open はこの環境変数を使ってデスクトップ環境に含まれているユーザーフレンドリーなファイルオープナアプリケーションを選択します。この機能を使うにはインストールする必要があるパッケージが存在します。GNOME の場合、libgnome が必要です。Xfce の場合、exo が必要です。使用される DE 変数の値: gnome, kde, xfce, lxde, mate
DE 環境変数はウィンドウマネージャを起動する前にエクスポートする必要があります。例:
~/.xinitrc
export DE="xfce"
exec openbox
This will make xdg-open use the more user-friendly exo-open, because it assumes it is running inside Xfce. Use exo-preferred-applications for configuring.
  • DESKTOP_SESSIONDE と似ていますが、LXDE デスクトップ環境で使われています: DESKTOP_SESSIONLXDE に設定されている場合、xdg-openpcmanfm のファイル関連付けを使います。
  • PATH contains a colon-separated list of directories in which your system looks for executable files. When a regular command (e.g., ls, rc-update or ic|emerge) is interpreted by the shell (e.g., bash or zsh), the shell looks for an executable file with the same name as your command in the listed directories, and executes it. To run executables that are not listed in PATH, the absoute path to the executable must be given: /bin/ls.
ノート: It is advised not to include the current working directory (.) into your PATH for security reasons, as it may trick the user to execute vicious commands.
  • HOME contains the path to the home directory of the current user. This variable can be used by applications to associate configuration files and such like with the user running it.
  • PWD contains the path to your working directory.
  • OLDPWD contains the path to your previous working directory, that is, the value of PWD before last cd was executed.
  • SHELL contains the name of the running, interactive shell, e.g., bash
  • TERM contains the name of the running terminal, e.g., xterm
  • PAGER contains command to run the program used to list the contents of files, e.g., /bin/less.
  • EDITOR contains the command to run the lightweight program used for editing files, e.g., /usr/bin/nano. For example, you can write an interactive switch between gedit under X or nano in this example):
export EDITOR="$(if [[ -n $DISPLAY ]]; then echo 'gedit'; else echo 'nano'; fi)"
  • VISUAL contains command to run the full-fledged editor that is used for more demanding tasks, such as editing mail (e.g., vi, vim, emacs etc).
  • MAIL contains the location of incoming email. The traditional setting is /var/spool/mail/$LOGNAME.
  • BROWSER contains the path to the web browser. Helpful to set in an interactive shell configuration file so that it may be dynamically altered depending on the availability of a graphic environment, such as X:
if [ -n "$DISPLAY" ]; then
    export BROWSER=firefox
else 
    export BROWSER=links
fi
  • ftp_proxy and http_proxy contains FTP and HTTP proxy server, respectively:
ftp_proxy="ftp://192.168.0.1:21"
http_proxy="http://192.168.0.1:80"
  • MANPATH contains a colon-separated list of directories in which man searches for the man pages.
ノート: In /etc/profile, there is a comment that states "Man is much better than us at figuring this out", so this variable should generally be left as default, i.e. /usr/share/man:/usr/local/share/man
  • INFODIR contains a colon-separated list of directories in which the info command searches for the info pages, e.g., /usr/share/info:/usr/local/share/info
  • TZ can be used to to set a time zone different to the system zone for a user. The zones listed in /usr/share/zoneinfo/ can be used as reference, for example TZ="/usr/share/zoneinfo/Pacific/Fiji"

変数の定義

Systemd/ユーザー#環境変数も見て下さい。

グローバル

Most Linux distributions tell you to change or add environment variable definitions in /etc/profile or other locations. Be sure to maintain and manage the environment variables and pay attention to the numerous files that can contain environment variables. In principle, any shell script can be used for initializing environmental variables, but following traditional UNIX conventions, these statements should be only be present in some particular files.

The following files should be used for defining global environment variables on your system: /etc/profile, /etc/bash.bashrc and /etc/environment. Each of these files has different limitations, so you should carefully select the appropriate one for your purposes.

  • /etc/profile initializes variables for login shells only. It does, however, run scripts and can be used all Bourne shell compatible shells.
  • /etc/bash.bashrc initializes variables for interactive shells only. It also runs scripts but (as its name implies) is Bash specific.
  • /etc/environment is used by the PAM-env module and is agnostic to login/non-login, interactive/non-interactive and also Bash/non-Bash, so scripting or glob expansion cannot be used. The file only accepts variable=value pairs.

In this example, we add ~/bin directory to the PATH for respective user. To do this, just put this in your preferred global environment variable config file (/etc/profile or /etc/bash.bashrc):

# If user ID is greater than or equal to 1000 & if ~/bin exists and is a directory & if ~/bin is not already in your $PATH
# then export ~/bin to your $PATH.
if [[ $UID -ge 1000 && -d $HOME/bin && -z $(echo $PATH | grep -o $HOME/bin) ]]
then
    export PATH=$HOME/bin:${PATH}
fi

ユーザーごと

環境変数をグローバルに定義したくないという時もあるでしょう。例えば、PATH/home/my_user/bin を追加したいが、システム上の他のユーザーには同じ PATH を使って欲しくないという場合が考えられます。様々なファイルを使うことでローカルに環境変数を定義することができます:

  1. シェルの設定ファイル、例えば Bash#設定ファイルZsh#設定ファイル
  2. ~/.profile is used by many shells as fallback, see wikipedia:Unix_shell#Configuration_files_for_shells.
  3. ~/.pam_environment is the user specific equivalent of /etc/environment, used by PAM-env module. See pam_env(8) for details.

To add a directory to the PATH for local usage, put following in ~/.bash_profile:

export PATH="${PATH}:/home/my_user/bin"

To update the variable, re-login or source the file: $ source ~/.bash_profile.

グラフィカルアプリケーション

GUI アプリケーションの環境変数を設定したいときは、xinitrc (ディスプレイマネージャを使う場合 xprofile) に変数を記述することができます。例:

~/.xinitrc
export PATH="${PATH}:~/scripts"
export GUIVAR=value

セッションごと

Sometimes even stricter definitions are required. One might want to temporarily run executables from a specific directory created without having to type the absolute path to each one, or editing ~/.bash_profile for the short time needed to run them.

In this case, you can define the PATH variable in your current session, combined with the export command. As long as you do not log out, the PATH variable will be using the temporary settings. To add a session-specific directory to PATH, issue:

$ export PATH="${PATH}:/home/my_user/tmp/usr/bin"

参照