General

Environment variables are named strings available to all applications. Variables are used to adapt each application's behavior to the environment it is running in. You might define paths for files, language options, and so on. You can see each application's manual to see what variables are used by that application.

That said, there are several standard variables in Linux environments:

  • PATH = Colon separated list of directories to search for binaries.
  • HOME = Current user's home directory.
  • USER = Current logged in user's name.
  • SHELL = The current shell.
  • PS1 = Defines shell's command prompt.
  • EDITOR = defines the user's preferred text editor.
  • (please feel free to add more)

To see your currently defined variables, open up your terminal and type the command env

Variables are defined with name-value pairs: "NAME = any string as value". The variable name is usually in capital letters. Anything that follows the equal-sign is considered the variable's value until the terminating line feed character. Any whitespace around the equal-sign is ignored. Variables can be defined ad hoc in a terminal by writing the appropriate command. In Bash this would be 'export MYVAL=Hello world'. In this case the variable stays defined until the end of the terminal session.

When working in shells or shell scripts: If you do not want to over-write the previous value of the variable, include the variable name into the new definition. E.g. in Bash: export PATH=$PATH:~/bin. This example shows how to append the bin directory in the user's home directory onto the PATH environment variable.

In most cases it is most convenient to store these variables in a configuration file that is read during system boot and user login so that they are available automatically. Unfortunately this not always as easy as it sounds. Why? For a couple of reasons:

  1. Environment variables are inherited; i.e., the parent program sets the environment for the child process. You need to configure the parent's settings so that it passes it on for all its children.
  2. Various shells and window managers are the parent programs we are looking for but each of them reads a different configuration file (dot file) when it starts.

So, with this knowledge we understand that we need to consider both the starting order of system processes and the configuration files they read when they are started. See the DotFiles page, or read on ...

Lets get to it! There are two ways you can run your Linux box: from text console or graphical user interface.

Using text console

Boot process in regards to environment variable definition when a text console (also called login shell) is used.

  1. At the end of boot the mother of all processes init is started. init's environment, including PATH, is defined in its source code and cannot be changed at run time.

  2. init runs the start-up scripts from /etc/init.d depending on the run level set in /etc/inittab. Since init's environment is very bare, the scripts define their required environment variables within themselves.

  3. init starts the text login process that waits for the user to log in. When the user logs in, the login process checks /etc/passwd to see what shell should be started for this particular user.

  4. The shell starts and reads its shell-specific configuration files. 
    1. Bash first reads /etc/profile to get values that are defined for all users. After reading that file, it looks for ~/.bash_profile', ~/.bash_login', and `~/.profile', in that order, and reads and executes commands from the first of these files that exists and is readable. b. (please fill in other shells as well)

Now the environment variables are ready to be used by the applications you start from the terminal.

Using graphical UI

Boot process in regards of environment variable definition when graphical login is used. (Information here is Gnome / GDM specific)

  1. At the end of booting, the mother of all processes -- init -- is started.

  2. init runs the start-up scripts from /etc/init.d depending on the run level set in /etc/inittab. Since init's environment is very bare, the scripts define required environment variables within themselves.

  3. Init starts the GDM display manager, which in turn will start the graphical login.
  4. When the user successfully logs in, GDM starts xsession, which reads the file /etc/gdm/Xsession and with it the environment variables for the user's session. The default version of the Xsession file first reads /etc/profile for global settings and then ~/.profile to add the user's individual settings.

Now the environment variables are set and used when programs are run in this session.

Quick guide

For the hasty who just need to get the system running, here is what you can do:

  • Put all global definitions, i.e. ones affecting all users into /etc/profile.

  • Insert all personal definitions into ~/.profile

  • Create or edit file ~/.bash_profile and include commands:

     if [ -f ~/.profile ]; then
    . ~/.profile
    fi

Notes and exceptions

startx from terminal

If you start X Window (the GUI) from a text console, your environment variables are already defined as explained above. However, the window manager may read the same files again (see below). This is usually not a problem, but you may get unexpected results, such as PATH having all entries listed twice.

Shell cascading

If you start another shell within the login shell (yes it is possible), the second one is a non-login shell. It will not read named start-up files but searches non-login start-up script from user's home directory instead. With Bash it is called ~/.bashrc. To avoid specifying same values in two places usually the login-shell start-up script ~/.bash_profile includes the ~/.bashrc at the end of its execution. To implement include following into your ~/.bash_profile:

if [ -f ~/.bashrc ]; then
. ~/.bashrc;
fi

terminal windows in X

If you start terminal / console window in graphical desktop environment it will be non-login terminal and it will read only the user's non-login start-up script. For Bash this is ~/.bashrc.

Using su

The su command is used to become another user during a login session. It is commonly used to get root permissions temporarily from normal session. su command resets your PATH environment value to one defined in /etc/login.defs by ENV_PATH and ENV_SUPATH variables. Please note that using Gnome helper gksu from Gnome panel by default uses su internally (i.e. you will "lose" your PATH if you do not configure it in login.defs).

Debian Environment Variables的更多相关文章

  1. CVE: 2014-6271、CVE: 2014-7169 Bash Specially-crafted Environment Variables Code Injection Vulnerability Analysis

    目录 . 漏洞的起因 . 漏洞原理分析 . 漏洞的影响范围 . 漏洞的利用场景 . 漏洞的POC.测试方法 . 漏洞的修复Patch情况 . 如何避免此类漏洞继续出现 1. 漏洞的起因 为了理解这个漏 ...

  2. How to keep Environment Variables when Using SUDO

    The trick is to add environment variables to sudoers file via sudo visudo command and add these line ...

  3. Environment Variables

    https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx Every process has an ...

  4. [Whole Web, Nods.js, PM2] Passing environment variables to node.js using pm2

    learn how to pass environment variables to your node.js app using the pm2 config file. This is usefu ...

  5. List environment variables from Command Prompt

    Request: List the environment variables from Command Promt To list one varibales , the syntax is lik ...

  6. [NPM] Execute npx commands with $npm_ Environment Variables

    We will incorporate npm specific environment variables when executing various npx commands. In our e ...

  7. How to set JAVA environment variables in Linux or CentOS

    How to set JAVA environment variables JAVA_HOME and PATH in Linux After installing new java (jdk or ...

  8. SSIS ->> Environment Variables

    SQL Server Integration Services(SSIS) 在2012版本引入了Environment Variables这个新特性.它允许我们为一个环境创建出一套变量用于为项目内的包 ...

  9. svn: None of the environment variables SVN_EDITOR...问题解决

    转:http://blog.163.com/lgh_2002/blog/static/44017526201046111856208/ 问题1: svn: Could not use external ...

随机推荐

  1. AngularJs学习第一课 Hello World

    首先先介绍一下:AngularJS是干什么的. AngularJS是为了克服HTML在构建应用上的不足而设计的.HTML是一门很好的为静态文本展示设计的声明式语言,但要构建WEB应用的话它就显得乏力了 ...

  2. linux常用命令之文件系统

    df df - report file system disk space usage 查看文件系统的使用清空 用法 df [-hi] [path] 选项 -h human readable ,以人类 ...

  3. css知多少(3)——样式来源与层叠规则

    上一节<css知多少(2)——学习css的思路>有几个人留言表示思路很好.继续期待,而且收到了9个赞,我还是比较欣慰的.没看过的朋友建议先去看看上一节. 这一节就开始实践上一节的思路! 1 ...

  4. 点击div全选中再点击取消全选div里面的文字

    想做一个就是点击一个div然后实现的功能是div里面的文字都成选中状态,然后就可以利用浏览器的自带的复制功能,任意复制在哪里去了 在网上百度了一下 然后网上的答案感觉很大的范围 然后一些搜索 然后就锁 ...

  5. WPF如何实现一个漂亮的页签导航UI

    最近看到一个比较漂亮的UI主界面,该UI是用左边的页签进行导航,比较有特色,就想着尝试用WPF来实现一下.经过一番尝试,基本上将UI设计图的效果用WPF程序进行了实现.下面介绍一下主要的思路: 1 U ...

  6. Kickoff - 创造可扩展的,响应式的网站

    Kickoff 是一个轻量级的前端框架,用于创建可扩展的,响应式的网站.作为前端开发人员,我们工作的类型越来越多样化.Kickoff 旨在帮助您在所有项目保持一致的结构和风格,无需添加其他框架. 在线 ...

  7. HTML5&CSS3经典动态表格

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  8. windows Python 3.4.3 安装图文

    1.去官网(https://www.python.org/downloads/)下载软件. 2.运行安装程序: 下一步 next. 下一步 next 全部选中,下一步 next. 安装中..来自:ht ...

  9. php随机生成指定长度的字符串 可以固定数字 字母 混合

    php 生成随机字符串 可以指定是纯数字 还是纯字母 或者混合的. 可以指定长度的. function rand_zifu($what,$number){ $string=''; for($i = 1 ...

  10. JS 事件代理

    事件处理器:onclick.onmouseover.... 在传统的事件处理中,你需要为每一个元素添加或者是删除事件处理器.然而,事件处理器将有可能导致内存泄露或者是性能下降——你用得越多这种风险就越 ...