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. Fedora 下安装codeblocks

    首先,安装codeblocks:yum install codeblocks* -y 然后安装gcc,gdb,g++:yum install gcc gdb gcc-c++ -y 然后安装gtk的一些 ...

  2. javascript模板引擎之artTemplate 学习笔记

    <div id="content"></div><div id="content1"></div><h1& ...

  3. hdu 4812 D Tree(树的点分治)

    D Tree Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/102400 K (Java/Others) Total ...

  4. Locally weighted linear regression(局部加权线性回归)

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 前面几篇博客主要介绍了线性回归的学习算法,那么它有什么不足的地方么 ...

  5. .htaccess和license文件编写

    1 .htaccess 1.1 文件的位置 默认情况下放置于根目录 1.2 .htaccess文件的编写 1.2.1 错误页面跳转 ErrorDocument + 错误码 + 跳转路径/提示文字 eg ...

  6. char *和char[]的区别,困扰很长时间了,总结下

    char c1[] = "hello";// char *c2 = "hello";// 区别1: c1是一个局部数组,c2是一个全局数组. 局部数组c1是局部 ...

  7. 不使用webview,用手机浏览器的android app

    需求:wap站在手机上以App的形式打开,但不要嵌套WebView,只能以浏览器打开 package com.gzz.whyinzi; import android.net.Uri; import a ...

  8. ZOJ 3829 贪心 思维题

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3829 现场做这道题的时候,感觉是思维题.自己智商不够.不敢搞,想着队友智商 ...

  9. Swift - 使用网格(UICollectionView)的自定义布局实现复杂页面

    网格UICollectionView除了使用流布局,还可以使用自定义布局.实现自定义布局需要继承UICollectionViewLayout,同时还要重载下面的三个方法: 1 2 3 4 5 6 7 ...

  10. Delphi中获取Unix时间戳及注意事项(c语言中time()是按格林威治时间计算的,比北京时间多了8小时)

    uses DateUtils;DateTimeToUnix(Now) 可以转换到unix时间,但是注意的是,它得到的时间比c语言中time()得到的时间大了8*60*60这是因为Now是当前时区的时间 ...