Linux學習 bash及其特性

2021-07-11 20:43:03 字數 4207 閱讀 3750

shell:外殼

gui:gnome,kde,xfce

cli:sh,csh,ksh,bash(bourne-again shell),tcsh,zsh

不同使用者啟動相同sh程式,但是是不同的程序。

程序:在每個程序看來,當前主機上只存在核心和當前程序。

程序是程式的副本,也是程式執行的例項,具有生命週期。

使用者工作環境

bash:不同使用者的工作環境設定可以不同。

# root

$user

shell:有子shell的概念。可以互動開啟。

bash:

1、命令歷史,命令補全

2、管道、重定向

3、命令別名

4、命令列編輯

5、命令列展開

6、檔名通配

7、支援變數

8、支援程式設計

特性:一、命令列編輯

游標跳**ctrl+a   跳到命令列首

ctrl+e  跳到命令行尾

ctrl+u   刪除游標至行首的內容

ctrl+k   刪除游標至行尾的內容

ctrl+左右箭頭  一次跳乙個單詞

ctrl+l  清屏

二、命令歷史

自動記錄使用過的命令,儲存在記憶體的一段緩衝區中。

使用 history 檢視命令歷史。

-c:清空命令歷史

-d offset [n]:刪除指定位置歷史列表。 history -d 300 20 

當使用者退出,會在使用者家目錄下追加此前的命令歷史到.bash_history隱藏檔案。

-w:將緩衝區的命令歷史至歷史檔案中    

命令補全:

在path環境變數下搜尋補全。

路徑補全:

環境變數:

path:命令搜尋路徑

histsize:命令歷史大小

[root@localhost ~]# echo $histsize

1000

命令歷史的使用技巧:

!n  :執行命令歷史中的第n條命令

324  history

325 ls -a

326 echo $histsize

327 history

[root@localhost ~]# !326

echo $histsize

1000

328  echo $histsize

329 history

[root@localhost ~]# !-2

echo $histsize

1000

[root@localhost ~]# !!

echo $histsize

1000

[root@localhost ~]# !!

echo $histsize

1000

!string: 執行命令歷史中最近乙個以指定字串開頭的命令。

!$:引用前乙個命令的最後乙個引數。

esc, . :按下esc,再按『點』,引用最後乙個引數。  

三、命令別名

alias cmdalias='command [option] [arguments]'

僅在當前shell程序的生命週期有用。別名的有效範圍僅為當前shell程序。

[root@localhost ~]# alias cls=clear
撤銷別名

[root@localhost ~]# unalias cls
\cmd : 來使用命令本身,而不是別名(當別名和命令名稱一樣時)

當在~/.bashrc下編輯別名後,可使用 source  .bashrc 命令執行bash。直接使用別名

四、命令替換  $(),或反引號 `command`

把命令中某個子命令替換為其執行結果的過程。 

[root@localhost sysconfig]# echo "the current directory is $(pwd)."

the current directory is /etc/sysconfig.

[root@localhost sysconfig]# cd /root/

[root@localhost ~]# echo "the current directory is $(pwd)."

the current directory is /root.

[root@localhost ~]# echo "the current directory is `pwd`."

the current directory is /root.

建立 file-2016-5-17-18-52. txt

[root@localhost ~]# touch ./file-$(date +%f-%h-%m-%s).txt

bash支援的引號:

``:反引號,命令替換

"":弱引用,可以實現變數替換,將變數名替換為變數值

'' :強引用,不完成變數替換。

五、檔名通配,globing

*:匹配任意長度的任意字元

?:匹配任意單個字元

[ ]:匹配指定範圍內的任意單個字元

[abc],[a-z],[a-z],[0-9],[a-za-z]

[^]:匹配指定範圍外的任意單個字元

[root@localhost test]# touch a123 abc ab123 xyz x12 xyz123

[root@localhost test]# ls

a123 ab123 abc x12 xyz xyz123

[root@localhost test]# ls a*

a123 ab123 abc

[root@localhost test]# ls a*3

a123 ab123

[root@localhost test]# ls *y*

xyz xyz123

[root@localhost test]# touch helloy123

[root@localhost test]# ls *y*

helloy123 xyz xyz123

[root@localhost test]# touch y123

[root@localhost test]# ls *y*

helloy123 xyz xyz123 y123

[root@localhost test]# ls ?y*

xyz xyz123

[root@localhost test]# touch 123

[root@localhost test]# ls [a-za-z]*[0-9]

a123 ab123 helloy123 x12 xyz123 y123

[root@localhost test]# ls [^0-9]*

a123  ab123  abc  helloy123  x12  xyz  xyz123  y123

[:space:] : 所有的空白字元  [[:space:]]  空白字元

[:punct:]  :所有 標點符號

[:lower:] : 小寫字母

[:upper:] : 大寫字母

[:alpha:] :大小寫字母

[:digit:] : 數字

[:alnum:]  : 數字和大小寫字母

[root@localhost test]# man 7 glob   //檢視

[root@localhost test]# ls [[:alpha:]]*[[:space:]]*[[:alpha:]] //匹配檔名中有空白字元的檔案

a b

[root@localhost test]# ls [[:alpha:]]*[^[:alpha:]]  //字母開頭,非字母結尾

a123 ab123 helloy123 x12 xyz123 y123

linux學習隨記 bash及其特性

1.程序 在每個程序看來,當前主機只存在核心和當前程序,程序時程式的別名,程序是程式的例項。2.ctrl a 命令列置首 ctrl e 命令列位 ctrl u 游標至命令列首 ctrl k 游標置行尾 3.history 命令歷史 命令歷史 n執行命令歷史第n條命令 4.bash支援的引號 弱引用 ...

bash及其特性

gui gnome,kde,xfce cli sh,csh,ksh,bash,tcsh,zsh 允許乙個使用者登入多次 程序 在每乙個程序看來,當前主機上只存在核心和當前程序 程序是程式的副本,程序是程式執行的例項 bash自身是外部命令 但是它帶有自身命令 bash shell,子shell 某些...

linux的學習之路 (五)bash及其特性

作業系統組成作用shell是離使用者最近的程式 shell 外殼 兩類 gui gnome,kde,xfce cli sh,csh,ksh,bash 都是程式,就是功能支援的不同而已 程序 在每個程序看來,當前主機只存在核心和當前程序 程序是程式的副本,程序是程式執行例項 不同的使用者,登陸系統的時...