Debian Environment Variables
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:
- 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.
- 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.
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.
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.
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.
- The shell starts and reads its shell-specific configuration files.
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)
At the end of booting, the mother of all processes -- init -- is started.
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.
- Init starts the GDM display manager, which in turn will start the graphical login.
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的更多相关文章
- CVE: 2014-6271、CVE: 2014-7169 Bash Specially-crafted Environment Variables Code Injection Vulnerability Analysis
目录 . 漏洞的起因 . 漏洞原理分析 . 漏洞的影响范围 . 漏洞的利用场景 . 漏洞的POC.测试方法 . 漏洞的修复Patch情况 . 如何避免此类漏洞继续出现 1. 漏洞的起因 为了理解这个漏 ...
- 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 ...
- Environment Variables
https://msdn.microsoft.com/en-us/library/windows/desktop/ms682653(v=vs.85).aspx Every process has an ...
- [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 ...
- List environment variables from Command Prompt
Request: List the environment variables from Command Promt To list one varibales , the syntax is lik ...
- [NPM] Execute npx commands with $npm_ Environment Variables
We will incorporate npm specific environment variables when executing various npx commands. In our e ...
- 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 ...
- SSIS ->> Environment Variables
SQL Server Integration Services(SSIS) 在2012版本引入了Environment Variables这个新特性.它允许我们为一个环境创建出一套变量用于为项目内的包 ...
- svn: None of the environment variables SVN_EDITOR...问题解决
转:http://blog.163.com/lgh_2002/blog/static/44017526201046111856208/ 问题1: svn: Could not use external ...
随机推荐
- JavaScript学习(3):函数式编程
在这篇文章里,我们讨论函数式编程. 什么是函数式编程?根据百度百科的描述,“函数式编程是种编程典范,它将电脑运算视为函数的计算.函数编程语言最重要的基础是 λ 演算(lambda calculus). ...
- 国际化支持(I18N)
本章译者:@nixil 使用国际化支持(I18N)能够使你的应用根据用户所在地区的不同选择不同的语言.下面介绍如何在引用中使用国际化. 只允许使用UTF-8 Play只支持UTF-8一种字符编码.这是 ...
- 12款支持移动设备的响应式 WordPress 主题
响应式和现代设计风格的多用途 WordPress 主题与能够非常灵活的适应所有设备.而高级主题能够更大可能性的轻松定制.所有的主题是完全响应式的,您可以从主题选项中启用响应模式. 今天,这个列表收集了 ...
- sizzle源码分析 (2)ID 类 tag querySelectorAll 快速匹配
不是所有的选择器都需要去分词,生成相应的匹配函数,这样流程比较复杂,当浏览器具备原生的方法去匹配元素是,没有理由不优先匹配,下面看看进入Sizzle后,它是怎么优先匹配这些元素的: function ...
- MyEclipse 2015免费在线公开课,2月5日开讲
MyEclipse 2015免费在线公开课,2月5日开讲,由MyEclipse官方高级PM Brian Fernandes 主讲. 主讲内容: 更好地支持javascript和技术模块 全新的REST ...
- Android中View的绘制过程 onMeasure方法简述 附有自定义View例子
Android中View的绘制过程 onMeasure方法简述 附有自定义View例子 Android中View的绘制过程 当Activity获得焦点时,它将被要求绘制自己的布局,Android fr ...
- git 删除远程源,新增加源
git remote remove origin git remote add origin git@XXXX
- Android 样式和主题(style & theme)
Android 样式 android中的样式和CSS样式作用相似,都是用于为界面元素定义显示风格,它是一个包含一个或者多个view控件属性的集合.如:需要定义字体的颜色和大小. 在CSS中是这样定义的 ...
- sql 如果关联表 没有值 设置 默认值
SELECT *FROM ( SELECT t.task_name, t.status AS task_status, coalesce( r.task_ref_id, 999 ) AS task_ ...
- Xshell与securecrt之间不同
现在比较受欢迎的终端模拟器软件当属xshell和securecrt了,现在就客观的分析一下两款软件,以便更好选择. 一.功能对比1.1Xshell功能 支持布局切换 可调整执行顺序 提供多标签功能 对 ...