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的環境變量所在的記憶塊導入自己的環境變量塊中