標籤:bash shell 變數
本文的結構:bash shell簡介-->變數和變數操作、數組-->bash的環境設定檔分析
什麼是shell:
什麼是bash
[[email protected] ~]# cat /etc/shells // 檔案記錄了所有合法的shell
/bin/sh
/bin/bash
/sbin/nologin
/bin/dash
/bin/tcsh
/bin/csh
[[email protected] ~]# cat /etc/passwd
root:x:0:0:root:/root:/bin/bash
bin:x:1:1:bin:/bin:/sbin/nologin
daemon:x:2:2:daemon:/sbin:/sbin/nologin
adm:x:3:4:adm:/var/adm:/sbin/nologin
bash shell 功能
1:最多1000條
2:[-c 刪除目前history] [-a 同步進檔案] [-n 最近n條記錄]
3:記錄在~/.bash_history 檔案中中(前一次登入以前的命令,這一次的緩衝在記憶體中)
linux下的變數
1:什麼是變數:存在於記憶體空間中的,一組文字或符號組成,用於替代一些設定或資料,特別是複雜或變動的資料
2:變數設定規則
[[email protected] ~]# lang="lang is $LANG" //雙引號,保持特性
[[email protected] ~]# echo $lang
lang is en_US.UTF-8
[[email protected] ~]# lang=‘lang is $LANG‘ //單引號,純文字
[[email protected] ~]# echo $lang
lang is $LANG
[[email protected] ~]# version=$(uname -r) //命令執行結果作為變數作為外部輸入資訊
[[email protected] ~]# echo $version
2.6.32-431.el6.x86_64
[[email protected] ~]# version=`uname -r`
[[email protected] ~]# echo $version
2.6.32-431.el6.x86_64
${變數#} 從頭開始刪掉符合#後面替換文字“最短的那個”
${變數##} 從頭開始刪掉符合#後面替換文字“最長的那個”
${變數%} 從後面開始刪掉符合#後面替換文字“最短的那個”
${變數%%} 從後面開始刪掉符合#後面替換文字“最長的那個”
${變數/A/B} 將第一個A替換成B
${變數/ /A/B} 將所有的A替換成B
aname=${name-newname} // 若name 不存在,則改為newname
aname=${name:-newname} // 若name 不存在或為空白,則改為newname
aname=${name=newname} // 直接name改為newname
[[email protected] ~]# version="$version"hello
[[email protected] ~]# echo $version
2.6.32-431.el6.x86_64hello
[[email protected] ~]# version=${version}hello
[[email protected] ~]# echo $version
2.6.32-431.el6.x86_64hellohello
3:環境變數
4:語系變數 locale (影響顯示和結果)
[[email protected] ~]# locale //usr//lib//locale
LANG=en_US.UTF-8 //主語言設定,其他語系變數會被這兩個變數代替(LANG LC_ALL),一般設定LANG就ok
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
[[email protected] ~]# cat /etc/sysconfig/i18n
LANG="en_US.UTF-8"
SYSFONT="latarcyrheb-sun16"
5:變數讀取:read [[email protected] ~]# read atest
this is a test
[[email protected] ~]# echo $atest
this is a test
[[email protected] ~]# read -p "your name :" named //-p提示符 -t 秒數
your name :boxin
[[email protected] ~]# echo $named
boxin
6:數組:例子說明,關聯和一般數組差別在於:索引從0開始的數字變成自訂
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6E/CC/wKiom1WIFn-jjTVdAACROT-ZCVg613.jpg" style="float:none;" title="Image.png" alt="wKiom1WIFn-jjTVdAACROT-ZCVg613.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6E/C8/wKioL1WIGDehKaRBAABbLpt8X74539.jpg" style="float:none;" title="Image1.png" alt="wKioL1WIGDehKaRBAABbLpt8X74539.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M02/6E/CC/wKiom1WIFoCTXjtTAAA6OwRhXhY447.jpg" style="float:none;" title="Image2.png" alt="wKiom1WIFoCTXjtTAAA6OwRhXhY447.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6E/C8/wKioL1WIGDeBikXQAADRsiz2hbA832.jpg" style="float:none;" title="Image3.png" alt="wKioL1WIGDeBikXQAADRsiz2hbA832.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M00/6E/CC/wKiom1WIFoCQ0AOHAABOf5l6v6w192.jpg" style="float:none;" title="Image4.png" alt="wKiom1WIFoCQ0AOHAABOf5l6v6w192.jpg" />
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6E/C9/wKioL1WIGDexXuFdAABPr_rvBYc652.jpg" style="float:none;" title="Image5.png" alt="wKioL1WIGDexXuFdAABPr_rvBYc652.jpg" />
bash的環境設定檔
結構圖:
650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/6E/CC/wKiom1WIFoDjxkT4AADTQDjPLNw839.jpg" style="float:none;" title="Image6.png" alt="wKiom1WIFoDjxkT4AADTQDjPLNw839.jpg" />
登陸歡迎資訊
登入前 /etc/issue
登入後 /etc/motd
login shell
non-login shell
/etc/profile(系統整體設定,login shell會調用)調用以下三個檔案目錄
/etc/inputre(使用者有沒有自訂按鍵功能,TAB鍵等)
/etc/profile.d/*.sh(目錄下.sh 結尾的檔案會被調用,所有使用者共用命令別名)
/etc/sysconfig/i18n(語系設定)
~/.bash_profile(個人設定檔,login shell才會讀)
~/.bashrc(根據UID 規範 umask值 和 PS1變數,no-login shell會讀)
讀取/etc/profile.d/*.sh
讀取/etc/bashrc(redhat 才有)
本文出自 “Call me Boxin” 部落格,請務必保留此出處http://boxinknown.blog.51cto.com/10435935/1664229
Bash shell和變數詳解