Linux环境变量简介

1      X86 / x64

想要知道当前系统是x86还是x64,可以使用命令:

uname -a


X86系统下,你会得到:

Linux discworld 2.6.38-8-generic #42-UbuntuSMP Mon Apr 11 03:31:50 UTC 2011 i686i686 i386 GNU/Linux

 

X64系统下,你会得到:

Linux discworld 2.6.38-8-generic #42-UbuntuSMP Mon Apr 11 03:31:50 UTC 2011 x86_64 x86_64x86_64 GNU/Linux

 

你也可以使用:

uname -i


 来直接得到系统关键字。

2      检查可执行文件是32位还是64位

使用file指令,如:

file /usr/bin/gedit

 

你能够得到:

/usr/bin/gedit: ELF 64-bit LSB executable, x86-64, version 1 (SYSV), dynamicallylinked (uses shared libs), for GNU/Linux 2.6.24,BuildID[sha1]=0x5a388215eb6f60b420fc3b6d68ec52d563071f84, stripped

3      环境变量和Shell变量

 

环境变量:适用于当前shell,及其子shell和子进程。

 

Shell变量:仅仅适用于当前shell。通常用于存储临时数据,如当前目录。

 

4      打印环境变量

4.1  打印所有或者部分环境变量

printenv

如果内容太多,一屏看不完,可以分屏显示:

printenv | more

或者:

printenv | less

 

也可以输出到文件:

printenv > env.txt 

4.2  打印指定名字的变量

 printenv SHELL 

4.3  只打印导出的环境变量

env 

在都不带参数情况下,此命令和printenv的输出一样。

 

env指令的一个主要用途是以指定的环境变量设置来执行命令:

env VAR1=”blablabla” command_to_run command_options


 

4.4  打印Shell变量的名字和值

此命令打印所有的shell变量,环境变量,本地变量和shell函数:

set 

如果不想打印shell函数,则使用:

(set -o posix; set)


 

比较env和set的差异,可以获得所有的shell变量:

comm -23 <(set -o posix; set | sort) <(env | sort)


 

不过,由于set命令输出的值带双引号,而env输出的值不带双引号,还是会有一小部分环境变量被包括在上面命令的输出中。

 

5      常用环境变量

Here are some common environmentalvariables that you will come across:

 

-         SHELL: This describes the shell thatwill be interpreting any commands you type in. In most cases, this will be bashby default, but other values can be set if you prefer other options.

-         TERM: This specifies the type ofterminal to emulate when running the shell. Different hardware terminals can beemulated for different operating requirements. You usually won't need to worryabout this though.

-         USER: The current logged in user.

-         PWD: The current working directory.

-         OLDPWD: The previous working directory.This is kept by the shell in order to switch back to your previous directory byrunning cd -.

-         LS_COLORS: This defines color codes thatare used to optionally add colored output to the ls command. This is used todistinguish different file types and provide more info to the user at a glance.

-         MAIL: The path to the current user'smailbox.

-         PATH: A list of directories that thesystem will check when looking for commands. When a user types in a command,the system will check directories in this order for the executable.

-         LANG: The current language andlocalization settings, including character encoding.

-         HOME: The current user's home directory.

-         _: The most recent previously executedcommand.

 

In addition to these environmentalvariables, some shell variables that you'll often see are:

 

-         BASHOPTS: The list of options that wereused when bash was executed. This can be useful for finding out if the shellenvironment will operate in the way you want it to.

-         BASH_VERSION: The version of bash beingexecuted, in human-readable form.

-         BASH_VERSINFO: The version of bash, inmachine-readable output.

-         COLUMNS: The number of columns wide thatare being used to draw output on the screen.

-         DIRSTACK: The stack of directories thatare available with the pushd and popd commands.

-         HISTFILESIZE: Number of lines of commandhistory stored to a file.

-         HISTSIZE: Number of lines of commandhistory allowed in memory.

-         HOSTNAME: The hostname of the computerat this time.

-         IFS: The internal field separator toseparate input on the command line. By default, this is a space.

-         PS1: The primary command promptdefinition. This is used to define what your prompt looks like when you start ashell session. The PS2 is used to declare secondary prompts for when a commandspans multiple lines.

-         SHELLOPTS: Shell options that can be setwith the set option.

-         UID: The UID of the current user.

 

6      创建Shell变量

TEST_VAR='Hello World!'


使用单引号,因为这个值里面包含一个空格。另外,感叹号是特殊字符,必须用单引号包起来。

 

如此即创建了一个Shell变量,仅仅在当前shell有用,并且此变量不会传递到子进程。

 

检查此变量是否存在:

set | grep TEST_VAR 

你应该能看到如下结果:

TEST_VAR='Hello World!'

 

或者:

echo $TEST_VAR

 

你应该能看到:

Hello World!

 

而如果使用printenv,则看不到此变量,因为此变量不是环境变量:

printenv | grep TEST_VAR

 

创建子shell,可以看到,此变量无法在子shell中访问:

bash

echo $TEST_VAR

 

退出子shell,回到我们的原始shell:

exit

  

7      创建环境变量

要想创建环境变量,我们得使用export指令。

 

把上面的shell变量导出到环境:

export TEST_VAR

 

如此一来,此变量即可在子进程和子shell中访问了!

 

也可以在导出时创建:

export NEW_VAR="Testing export"

 

注意导出环境变量只会影响子进程和子shell,而不会影响父shell。

 

8      把环境变量转换为shell变量


export -n TEST_VAR


9      删除环境变量或者shell变量


unset TEST_VAR


10            将环境变量存储为系统配置

10.1    Login, Non-Login, Interactive,Non-Interactive Shell

Linux中的shell有几种属性:

 

-         Login:以授权一个用户而开始shell就是login shell

n  此shell首先加载/etc/profile

n  然后:~/.bash_profile

n  ~/.bash_login

n  ~/.profile

n  完毕,不再加载其他任何文件

-         Non-Login:如果你从一个已登录的session来启动bash shell,则你启动了一个non-loginshell

n  此shell首先加载/etc/bash.bashrc

n  然后:~/.bashrc

-         Interactive:如果shell有一个输入输出终端(命令窗口),则为interactive shell

-         Non-Interactive:没有输入输出终端的shell,例如从命令行启动的一个脚本。

n  此shell从环境变量中读取名为:BASH_ENV的变量,找到此变量对应的文件后加载之

 

一个shell可以是Login/Non-Login和Interactive/Non-Interactive的任意组合。

 

10.2    存储环境变量到配置文件

绝大多数linux系统,都修改了登录配置,以便让login shell也去加载non-login 的shell配置文件,也就是:~/.bashrc。这意味着我们只需要修改~/.bashrc即可完成让所有的shell都支持我们修改的环境变量。

 

用文本编辑器打开~/.bashrc,增加以下代码:


export VARNAME=value


存储后,打开新的shell即可享受到新的配置。

 

当然,你也可以强行在当前shell执行~/.bashrc里的代码,从而更新当前shell的配置:


source ~/.bashrc


11            引用

-         https://www.digitalocean.com/community/tutorials/how-to-read-and-set-environmental-and-shell-variables-on-a-linux-vps

-         http://www.cyberciti.biz/faq/linux-list-all-environment-variables-env-command/

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值