您的位置:首页 > 运维架构 > Shell

shell的环境变量

2011-05-14 09:57 106 查看
1./etc/profile

用户登录时,自动读取/ect目录下profile文件,此文件包括

全局或局部环境变量

PATH信息

终端设置

安全命令

日期信息或放弃操作信息

2.stty用法

stty用于设置终端特性。要查询现在的stty选项,使用stty -a

$stty -a

stty的一个选项为-g,此选项允许以只读格式保存stty现有设置,便于以后重置回stty.

3.显示变量

使用echo命令可以显示单个变量,并在变量名前加$,例如:

$ DOLLAR=99

$ echo ${DOLLAR}

99

$ LAST_FILE=s2

$ echo ${LAST_FILE}

s2

$ ERROR="This is user: $LOGNAME"

$ echo ${ERROR}
This is user: dongjichao

4.清除变量

使用unset命令清除变量

$ PC=enterprise

$ echo ${PC}

enterprise

$ unset PC

$ echo ${PC}

$

5.显示所有本地shell变量

$ set | more

6.测试变量是否已经设置

$ echo "The file is ${FILES:?}"

FILES: parameter null or not set

7.显示环境变量

$ CONSOLE=tty1; export CONSOLE

$ echo $CONSOLE

tty1

使用env命令可以查看所有的环境变量

$ env

8.使用export导出变量

有一父进程

$ cat father.sh
#!/bin/sh
echo "this is the father"
FILM="A Few Good Men"
echo "I like the file :$FILM"
#call the child script
export FILM #去掉此句,在child.sh中变量$FILM则为空
child.sh
echo "back to father"
echo "and the file is :$FILM"

子进程为
$ cat child.sh
#!/bin/sh
echo "called from father.. i am the child"
echo "film name is :$FILM"
FILM="Die Hard"
echo "changing file to :$FILM"

运行father输出:

$ father.sh
this is the father
I like the file :A Few Good Men
called from father.. i am the child
film name is :A Few Good Men
changing file to :Die Hard
back to father
and the file is :A Few Good Men
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: