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的环境变量所在的记忆块导入自己的环境变量块中

bash学习之环境变量的更多相关文章

  1. Shell学习之环境变量配置文件(三)

    Shell学习之环境变量配置文件 目录 环境变量配置文件简介 环境变量配置文件作用 其他配置文件和登录信息 环境变量配置文件简介 环境变量配置文件简介 环境变量配置文件中主要是定义对系统操作环境生效的 ...

  2. NodeJS学习:环境变量

    简介 环境变量(environment variables) 不属于 NodeJS 范畴,它是操作系统用于设定执行环境的参数.会在程序运行时传递给应用程序. NodeJS 获取环境变量,是通过 glo ...

  3. Bash 中的环境变量

    在 Bash 里,可以通过 export 命令查看当前 Shell 进程的环境变量,这些环境变量一些是 Bash 自己创建的,还有一些是 Bash 从父进程继承来的,然而需要注意的是,父进程传给 Ba ...

  4. Shell学习笔记 - 环境变量配置文件(转)

    一.source命令 功能:在当前bash环境下读取并执行配置文件中的命令 1. 命令格式 source 配置文件  或  . 配置文件 2. 命令示例 [root@localhost ~]# sou ...

  5. Shell学习笔记 - 环境变量配置文件

    一.source命令 功能:在当前bash环境下读取并执行配置文件中的命令 1. 命令格式 source 配置文件  或  . 配置文件 2. 命令示例 [root@localhost ~]# sou ...

  6. linux 学习:环境变量设置

    一.临时环境变量 临时环境变量,只对当前打开的shell生效,shell关闭后,环境变量失效. 设置方法一: 分两步 MYPARA=hello export MYPARA 设置方法二:一步完成 exp ...

  7. Java基础学习-Path环境变量的配置

    1.为什么要进行Path环境变量的配置       程序的编译和执行需要使用到javac和java命令,所以只能在bin目录下写程序,而实际开发中,我们不可能将程序全部写到bin目录下,所以我们不许让 ...

  8. Python学习---Python环境变量安装问题0907

    问题背景: 重新安装操作系统后,原来的环境变量丢失[因Python3.5安装目录是E盘,文件还在,只是丢失了环境变量而已,添加即可] 问题解决: 方法一:使用cmd命令添加path环境变量 在cmd下 ...

  9. 乐字节Java学习03-path环境变量

    1. path环境变量的作用 保证javac命令可以在任意目录下运行. 2. path配置的两种方案: 方法 一如下: ①点击计算机->右键->属性 ②高级系统设置 ③高级—>环境变 ...

随机推荐

  1. Spring Tool Suite(简称STS)针对SimpleDateFormat.pase函数的实参值不做检验,异常直接默认值之

    Spring Tool Suite(简称STS)是 Spring 团队开发的一款基于Eclipse的IDE,旨在简化开发Spring MVC 应用的流程.可以自动生成spring相关的配置文件.比如a ...

  2. vim: vim快捷键

    0. 搜索字符串: 精确匹配查找单词 如果你输入 "/the",你也可能找到 "there". 要找到以 "the" 结尾的单词,可以用:/ ...

  3. HDU 3397 Sequence operation(线段树)

    HDU 3397 Sequence operation 题目链接 题意:给定一个01序列,有5种操作 0 a b [a.b]区间置为0 1 a b [a,b]区间置为1 2 a b [a,b]区间0变 ...

  4. Unity3D NGUI,uGUI总结

    跪求官方UI系统(2014年11月底已出,用原生的比用NGUI放心) uGUI注意点 1.要防止多个canvas叠加点击穿透,canvas里面的graphics raycaster调整到恰当选项 2. ...

  5. Swift - 给游戏添加背景音乐和音效(SpriteKit游戏开发)

    游戏少不了背景音乐和音效.下面我们通过创建一个管理音效的类,来实现背景音乐的播放,同时点击屏幕可以播放相应的音效. 声音管理类 SoundManager.swift 1 2 3 4 5 6 7 8 9 ...

  6. 安装ipvsadm报错

    server环境: [vagrant@localhost download]$ uname -a Linux RS1 2.6.18-238.el5 #1 SMP Thu Jan 13 15:51:15 ...

  7. Appium Server 传递Android参数

    Appium  server Capabilities 传递参数    Android 特定 Android Only Capability Description Values appActivit ...

  8. Oracle安装配置流程

    Oracle安装流程 第一次自己动手安装oracle,之前对oracle安装配置一窍不通,最后最终弄好.总结下. 1.  安装oracle10gserver端 2.  安装oracle10gclien ...

  9. C++中字母大写和小写转换实现的优化

    C++中字母大写和小写转换实现的优化 write by 九天雁翎(JTianLing) -- blog.csdn.net/vagrxie 讨论新闻组及文件 在本文中所有以转换为小写为例. 从推荐复用代 ...

  10. 爱的歌我uhegierhiuerh5怕哦一

    http://www.huihui.cn/share/8424421 http://www.huihui.cn/share/8424375 http://www.huihui.cn/share/842 ...