bash学习之环境变量

1、查看系统存在的环境变量env 和 export

env命令:查看环境变量
[CJP@CJP ~]$ env
HOSTNAME=CJP
SHELL=/bin/bash
HISTSIZE=1000
USERNAME=CJP
MAIL=/var/spool/mail/CJP
PATH=/home/CJP/qtsdk-2010.05/qt/bin:/usr/lib/qt-3.3/bin:/usr/local/bin:/usr/bin:/bin:/usr/local/sbin:/usr/sbin:/sbin:/home/CJP/bin
LANG=zh_CN.utf8
HOME=/home/CJP
[CJP@CJP ~]$

HOME:用户主文件夹。利用cd~ 或者 cd 就可以直接回到主文件夹,也正是因为利用了这个变量。
SHELL:目前环境使用的shell程序
HISTSIZE:与历史命令有关,设置被系统记录下来的曾经执行过的命令的条数
MAIL:使用mail命令时,系统读取的邮件信箱文件
PATH:执行文件查找的路径,目录之间用冒号(:)分隔,注意,其目录顺序影响执行。
LANG:语系数据。中文编码通常是zh_CN.gb2312 或者 zh_CN.UTF-8
RANDOM:随机数变量。随机数生成器,生成一个介于0~32767之间的数。
产生一个随机数
[CJP@CJP ~]$ echo $RANDOM
26043
产生一个0~9之间的随机数
[CJP@CJP ~]$ declare -i num=$RANDOM*10/32768; echo $num
8

2、用set查看所有变量
set可以显示环境变量,还可以显示其他的变量,如:bash操作接口有关的变量,用户自定义变量

bash主程序放置的路径
BASH=/bin/bash
bash版本信息
BASH_VERSINFO=([0]="4" [1]="1" [2]="2" [3]="1" [4]="release" [5]="i386-redhat-linux-gnu")
BASH_VERSION='4.1.2(1)-release'
颜色记录文件
COLORS=/etc/DIR_COLORS
当前终端机环境下,使用的字段的字符长度
COLUMNS=132
历史命令记录的配置文件,它是隐藏文件
HISTFILE=/home/CJP/.bash_history
保存的文件命令的最大记录条数
HISTFILESIZE=1000
目前环境下可记录的历史命令最大条数
HISTSIZE=1000
主机安装的软件主要类型
HOSTTYPE=i386
默认的分隔符
IFS=$' \t\n'
目前终端机下最大行数
LINES=24
安装的机器类型
MACHTYPE=i386-redhat-linux-gnu
与邮件有关,每60秒扫描一次信箱有无新信
MAILCHECK=60
上个工作目录,也就是在这之前进入的工作目录
OLDPWD=/etc
操作系统的类型
OSTYPE=linux-gnu
父进程的PID
PPID=2794
命令提示符,可以被修改
PS1='[\u@\h \W]\$ '
使用转义字符,第二行出现的提示符
PS2='> '
自定义变量
name='CJP'\''s name'
num=8
var=CJP

3、PS1:提示符的设置
[CJP@CJP ~]$ echo $PS1
[\u@\h \W]\$

可以修改反斜杠后的数据,设置PS1的特殊功能
[CJP@CJP ~]$ PS1="[\u@\h \w \A #\#]\$"
[CJP@CJP ~ 15:04 #32]$echo $PS1
[\u@\h \w \A #\#]$
[CJP@CJP ~ 15:04 #33]$

\u     目前用户的帐号名称
\h     仅取主机名在第一个小数点之前的名字
\w     完整的工作目录名称,由根目录写起的目录名称
\A     24小时格式的时间HH:MM
\#     执行的第几个命令
\$     提示符,如果是root(PID=0),提示符为#,其他的为$

如下是man bash得到的说明:
When executing interactively, bash displays the primary prompt PS1 when it is ready to read a command, and the
secondary prompt PS2 when it needs more input to complete a command. Bash allows these prompt strings to be
customized by inserting a number of backslash-escaped special characters that are decoded as follows:
\a     an ASCII bell character (07)
\d     the date in "Weekday Month Date" format (e.g., "Tue May 26")
\D{format}
         the format is passed to strftime(3) and the result is inserted into the prompt string; an empty
         format results in a locale-specific time representation. The braces are required
\e     an ASCII escape character (033)
\h     the hostname up to the first ‘.’
\H     the hostname
\j       the number of jobs currently managed by the shell
\l       the basename of the shell’s terminal device name
\n     newline
\r      carriage return
\s     the name of the shell, the basename of $0 (the portion following the final slash)
\t      the current time in 24-hour HH:MM:SS format
\T     the current time in 12-hour HH:MM:SS format
\@   the current time in 12-hour am/pm format
\A    the current time in 24-hour HH:MM format
\u     the username of the current user
\v     the version of bash (e.g., 2.00)
\V    the release of bash, version + patch level (e.g., 2.00.0)
\w    the current working directory, with $HOME abbreviated with a tilde (uses the value of the
        PROMPT_DIRTRIM variable)
\W   the basename of the current working directory, with $HOME abbreviated with a tilde
\!      the history number of this command
\#     the command number of this command
\$     if the effective UID is 0, a #, otherwise a $
\nnn the character corresponding to the octal number nnn
\\     a backslash
\[     begin a sequence of non-printing characters, which could be used to embed a terminal control
       sequence into the prompt
\]     end a sequence of non-printing characters

4、关于shell的PID:$
$ 本身是一个变量,代表目前这个shell的线程代号,即PID(process ID)
[CJP@CJP ~]$ echo $$
6871

5、关于上次执行命令的回传码:?
? 也是一个变量,代表上一个执行命令所回传的值
如果上一个命令执行成功,返回0,否则传回非0数值

[CJP@CJP ~ 15:17 #34]$echo $SHELL
/bin/bash
[CJP@CJP ~ 15:17 #35]$echo $?
0
[CJP@CJP ~ 15:17 #36]$12name=CJP
bash: 12name=CJP: command not found
[CJP@CJP ~ 15:18 #37]$echo $?
127
[CJP@CJP ~ 15:18 #38]$echo $?

0


6、变量的有效范围

环境变量的数据可以被子进程引用,与内存配置有关系。

  • 启动shell时,操作系统会分配一块记忆块给shell使用,此内存里面的变量可以让子进程取用
  • 若在父进程利用export功能,可以让自定义变量内容写到上述内存记忆块中
  • 当加载另外一个shell时,子shell可以将父shell的环境变量所在的记忆块导入自己的环境变量块中



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值