关于Linux下的环境变量
一、交互式shell和非交互式shell
要搞清bashrc与profile的区别,首先要弄明白什么是交互式shell和非交互式shell,什么是login shell 和non-login shell。
【交互模式】
交互式模式就是shell等待你的输入,并且执行你提交的命令。这种模式被称作交互式是因为shell与用户进行交互。这种模式也是大多数用户非常熟悉的:登录、执行一些命令、签退。当你签退后,shell也终止了。
【非交互模式】
shell也可以运行在另外一种模式:非交互式模式。在这种模式下,shell不与你进行交互,而是读取存放在文件中的命令,并且执行它们。当它读到文件的结尾,shell也就终止了。
bashrc与profile都用于保存用户的环境信息,bashrc用于交互式non-loginshell,而profile用于交互式login shell。
二、几种环境变量配置文件
/etc/profile和/etc/bashrc都是对所有用户生效!
~/.bash_profile和~/.bashrc只对当前用户生效。~表示当前用户的家目录。
/etc/profile中设置的变量(全局)可以作用于任何用户,而~/.bashrc中设置的变量(局部)只能继承/etc/profile中的变量,它们是父子关系。
~/.bash_profile是交互式、login方式进入bash运行的;
~/.bashrc是交互式、non-login方式进入bash运行的;
1. /etc/profile
该文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行。并从/etc/profile.d目录的配置文件中搜集shell的设置。
[root@CentOS6 ~]# more /etc/profile
# /etc/profile # System wide environment and startup programs, for login setup
# Functions and aliases go in /etc/bashrc # It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates. pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
} if [ -x /usr/bin/id ]; then
if [ -z "$EUID" ]; then
# ksh workaround
EUID=`id -u`
UID=`id -ru`
fi
USER="`id -un`"
LOGNAME=$USER
MAIL="/var/spool/mail/$USER"
fi # Path manipulation
if [ "$EUID" = "0" ]; then
pathmunge /sbin
pathmunge /usr/sbin
pathmunge /usr/local/sbin
else
pathmunge /usr/local/sbin after
pathmunge /usr/sbin after
pathmunge /sbin after
fi HOSTNAME=`/bin/hostname 2>/dev/null`
HISTSIZE=1000
if [ "$HISTCONTROL" = "ignorespace" ] ; then
export HISTCONTROL=ignoreboth
else
export HISTCONTROL=ignoredups
fi export PATH USER LOGNAME MAIL HOSTNAME HISTSIZE HISTCONTROL # By default, we want umask to get set. This sets it for login shell
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi for i in /etc/profile.d/*.sh ; do
if [ -r "$i" ]; then
if [ "${-#*i}" != "$-" ]; then
. "$i"
else
. "$i" >/dev/null 2>&1
fi
fi
done unset i
unset -f pathmunge
2. /etc/bashrc
为每一个运行bash shell的用户执行此文件。当bash shell被打开时,该文件被读取。
# /etc/bashrc # System wide functions and aliases
# Environment stuff goes in /etc/profile # It's NOT a good idea to change this file unless you know what you
# are doing. It's much better to create a custom.sh shell script in
# /etc/profile.d/ to make custom changes to your environment, as this
# will prevent the need for merging in future updates. # are we an interactive shell?
if [ "$PS1" ]; then
if [ -z "$PROMPT_COMMAND" ]; then
case $TERM in
xterm*)
if [ -e /etc/sysconfig/bash-prompt-xterm ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-xterm
else
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\007" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
fi
;;
screen)
if [ -e /etc/sysconfig/bash-prompt-screen ]; then
PROMPT_COMMAND=/etc/sysconfig/bash-prompt-screen
else
PROMPT_COMMAND='printf "\033]0;%s@%s:%s\033\\" "${USER}" "${HOSTNAME%%.*}" "${PWD/#$HOME/~}"'
fi
;;
*)
[ -e /etc/sysconfig/bash-prompt-default ] && PROMPT_COMMAND=/etc/sysconfig/bash-prompt-default
;;
esac
fi
# Turn on checkwinsize
shopt -s checkwinsize
[ "$PS1" = "\\s-\\v\\\$ " ] && PS1="[\u@\h \W]\\$ "
# You might want to have e.g. tty in prompt (e.g. more virtual machines)
# and console windows
# If you want to do so, just add e.g.
# if [ "$PS1" ]; then
# PS1="[\u@\h:\l \W]\\$ "
# fi
# to your custom modification shell script in /etc/profile.d/ directory
fi if ! shopt -q login_shell ; then # We're not a login shell
# Need to redefine pathmunge, it get's undefined at the end of /etc/profile
pathmunge () {
case ":${PATH}:" in
*:"$1":*)
;;
*)
if [ "$2" = "after" ] ; then
PATH=$PATH:$1
else
PATH=$1:$PATH
fi
esac
} # By default, we want umask to get set. This sets it for non-login shell.
# Current threshold for system reserved uid/gids is 200
# You could check uidgid reservation validity in
# /usr/share/doc/setup-*/uidgid file
if [ $UID -gt 199 ] && [ "`id -gn`" = "`id -un`" ]; then
umask 002
else
umask 022
fi # Only display echos from profile.d scripts if we are no login shell
# and interactive - otherwise just process them to set envvars
for i in /etc/profile.d/*.sh; do
if [ -r "$i" ]; then
if [ "$PS1" ]; then
. "$i"
else
. "$i" >/dev/null 2>&1
fi
fi
done unset i
unset pathmunge
fi
# vim:ts=4:sw=4
3. ~/.bash_profile
每个用户都可使用该文件输入专用于自己使用的shell信息。当用户登录时,该文件仅仅执行一次。默认情况下, 他设置一些环境变量,执行用户的
~/.bashrc文件。
[root@CentOS6 ~]# more ~/.bash_profile
# .bash_profile # Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi # User specific environment and startup programs PATH=$PATH:$HOME/bin:/usr/local/mysql/bin export PATH
4.~/.bashrc
该文件包含专用于你的bash shell的bash信息。当登录时及每次打开新的shell时,该文件被读取。
每个用户都有一个~/.bashrc文件,在用户目录下。
[root@CentOS6 ~]# more ~/.bashrc
# .bashrc # User specific aliases and functions alias rm='rm -i'
alias cp='cp -i'
alias mv='mv -i' # Source global definitions
if [ -f /etc/bashrc ]; then
. /etc/bashrc
fi
5. ~/.bash_logout
当每次退出系统时(退出bash shell),执行该文件。
[root@CentOS6 ~]# more ~/.bash_logout
# ~/.bash_logout
关于Linux下的环境变量的更多相关文章
- hadoop搭建杂记:Linux下JDK环境变量的设置(三种配置环境变量的方法)
Linux下JDK环境变量的设置(三种配置环境变量的方法) Linux下JDK环境变量的设置(三种配置环境变量的方法) ①修改/etc/profile文件 如果你的计算机仅仅作为开发使用时推荐使用这种 ...
- linux下配置环境变量方式
linux下配置环境变量有多种方式,下面简述之 方式1.编辑 /etc/profile 文件,增加如下内容 JAVA_HOME=/usr/local/jdk1. export JAVA_HOME PA ...
- linux 下PATH环境变量
环境变量简介 什么是环境变量呢?简要的说,就是指定一个目录,运行软件的时候,相关的程序将会按照该目录寻找相关文件. 在linux系统下,如果你下载并安装了应用程序,很有可能在键入它的名称时出现&quo ...
- 环境变量解释以及在Linux下的环境变量设置
一.环境变量解释 环境变量是什么? 引用百度百科里面的解释:环境变量是操作系统中一个具有特定名字的对象,它包含了一个或者多个应用程序所将使用到的信息.例如Windows系统中的path环境变量,当要求 ...
- linux下jdk环境变量配置深度分析----解决环境变量不生效的问题
1.linux下jdk环境变量配置 是否需要配置环境变量,主要看java -version 显示的版本是否为你期望的版本 1.1 不需要配置环境变量的情况 使用java -version查看,版本显示 ...
- Linux下配置环境变量—— .bashrc 和 /etc/profile
转载:https://blog.csdn.net/sun8112133/article/details/79901527 首先简单说一下什么是环境变量?环境变量简单的说就是当前环境下的参数或者变量.如 ...
- linux下的环境变量
环境变量有时候要查找,但是经常忘记有哪些文件,现在做一个总结: /etc/profile 此文件为系统的每个用户设置环境信息,当用户第一次登录时,该文件被执行.并从/e ...
- Linux下配置环境变量的几个方法实例
场景:一般来说,配置交叉编译工具链的时候需要指定编译工具的路径,此时就需要设置环境变量.例如我的mips-linux-gcc编译器在“/opt/au1200_rm/build_tools/bin”目录 ...
- Linux下修改环境变量PATH
1.什么是环境变量(PATH) 在Linux中,在执行命令时,系统会按照PATH的设置,去每个PATH定义的路径下搜索执行文件,先搜索到的文件先执行. 我们知道查阅文件属性的指令ls 完整文件名为:/ ...
- 为什么Linux下的环境变量要用大写而不是小写
境变量的名称通常用大写字母来定义.实际上用小写字母来定义环境变量也不会报错,只是习惯上都是用大写字母来表示的. 首先说明一下,在Windows下是不区分大小写的,所以在Windows下怎么写都能获取到 ...
随机推荐
- Parenthesis UVALive - 4882 删除不必要的括号序列,模拟题 + 数据
https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_probl ...
- 打包google浏览器插件到本地
依次打开‘更多工具’--->'扩展程序',或者在google浏览器输入chrome://extensions/网址就可以打开已安装插件页面,在顶部选中‘开发者模式’后就会出现每个插件的ID,这个 ...
- Spark Mllib里如何将trainDara训练数据文件里提取第M到第N字段(图文详解)
不多说,直接上干货! 具体,见 Hadoop+Spark大数据巨量分析与机器学习整合开发实战的第13章 使用决策树二元分类算法来预测分类StumbleUpon数据集
- Spring之WebContext不使用web.xml启动 初始化重要的类源码分析(Servlet3.0以上的)
入口: org.springframework.web.SpringServletContainerInitializer implements ServletContainerInitializer ...
- P1791 线段覆盖
题目描述 已知数轴上0<N<10000条线段.每条线段按照端点Ai和Bi(Ai<>Bi,i=1..N)定义.端点坐标在(-999,999)内,坐标为整数.有些线段可能相交.编程 ...
- html5.0学习记录(一)——可拖动视频播放器
最近自己在重新学习html5新特性,了解到有视频标签和拖动标签,于是自己用这两个特性写了一个小demo,主要功能就是可以通过拖动视频来直接播放.效果图如下: 页面使用了<video>标签和 ...
- Python安装第三方库文件工具——pip
Python安装第三方库文件一般使用pip. 1.pip的安装 (1)下载pip 进入https://pypi.python.org/pypi/pip#downloads
- MySQL检查死锁简介
- 系统妈Ghost Win10 64位快速安装版 V2016年2月
系统妈Ghost win10 64位快速安装版 V2016年2月,更新了最新系统补丁,升级系统版本号为2016年2月份.这款累积更新补丁会取代之前的版本.本系统还附带最常用的装机必备软件.QQ等. 系 ...
- PBI DAX 中GroupBy
平时工作中经常会遇到Group By 的情形,用sql 写group by 很容易,在PBI中可以这样写: SUMMARIZE(表名,GroupBy Key ,"聚合列命名",DI ...