Bash shell和變數詳解

來源:互聯網
上載者:User

標籤:bash   shell   變數   

本文的結構:bash shell簡介-->變數和變數操作、數組-->bash的環境設定檔分析

什麼是shell

  • 作業系統是一組軟體,用於控制整個硬體和管理系統活動,若直接使用作業系統,操作不當會導致系統崩潰,使用者不可隨意操作,所以通過“shell”將我們輸入的命令與核心通訊,讓核心準確的控制硬體工作

  • shell是在作業系統上開發的應用程式,是使用者可操作的最外層,也就是“shell”殼

什麼是bash

  • Bourne Again SHell,Bourne shell的增強版

  • shell有很多版本,很多東西都一樣,出現後就會有很多的發展者,像作業系統有很多種是一樣的道理

  • shell種類查看:

     [[email protected] ~]# cat /etc/shells   // 檔案記錄了所有合法的shell                                                               
     /bin/sh       
     /bin/bash     
     /sbin/nologin
     /bin/dash
     /bin/tcsh
     /bin/csh

  • 使用者登入時根據 /etc/passwd 的記錄獲得shell

     [[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 功能

  • 命令記憶(history)

     1:最多1000條

     2:[-c 刪除目前history] [-a 同步進檔案] [-n 最近n條記錄]

     3:記錄在~/.bash_history 檔案中中(前一次登入以前的命令,這一次的緩衝在記憶體中)

  • 補全功能(Tab)

  • 別名(alias)

  • 作業控制、前台fg、後台bg

  • 指令碼(shell script)

  • 萬用字元(Wildcard)

  • 內建命令 type [-a 查看命令的所有來源] [-t 查看命令執行的首個來源] 命令

linux下的變數

1:什麼是變數:存在於記憶體空間中的,一組文字或符號組成,用於替代一些設定或資料,特別是複雜或變動的資料

  • 例1:PATH變數的值為 /usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin:/usr/local

  • 例2:MAIL變數用於訪問郵箱,當不同使用者登入shell時,MAIL就賦值為相應的使用者的郵箱,如boxin使用者 MAIL=/var/spool/mail/boxin,mail命令使用MAIL變數

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

  • 取消變數:unset 變數名

3:環境變數

  • env:(environment) 查看本shell下所有的環境變數

  • set:查看環境變數 + 自訂變數

  • export:將自訂變數改為環境變數

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

  • 取得bash 時需完整登入流程:輸入帳號密碼

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和變數詳解

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.