相信大家平時都有用history
命令來檢視命令歷史記錄,但是實際上history
命令並非只有這個功能,history
還有很多有用的功能。尤其是 bash 版本的history
命令,它所提供的功能比所有其他的 linux shellhistory
命令所提供的都要多。
bash 的歷史悠久,是乙個古老的 shell ,並且它還有乙個更古老的前身 the bourne shell (sh) 。因此,bash 的history
命令是所有的 linux shellhistory
命令中功能最豐富的。bash 版本的history
命令不僅支援反向搜尋、快速呼叫,還支援重寫歷史記錄等等功能。
善用 bashhistory
命令以上的這些功能都可以提高你的工作效率,因此,讓良許為你一一講解 bashhistory
命令以及它常用的功能:
history 是內建的命令
history
命令與許多其他的命令不同。你可能習慣於命令都作為可執行檔案放置在常見的系統級的位置,例如/usr/bin
,/usr/local/bin
或〜/ bin
。但是,內建的history
命令並不在你的環境變數path
儲存的路徑中的。
實際上,history
命令並沒有儲存在物理位置中:
$ which history
which: no history in [path]
history
其實是 shell 本身的乙個內建函式:
由於$ type history
history is a shell builtin
$ help history
history: history [-c] [-d offset] [n] or
history -anrw [filename] or
history -ps arg [arg...]
display or manipulate the history list.
[...]
history
是 shell 的內建函式,所以每種 shell 的history
函式都是獨一無二的。因此,你在 bash 中能使用的功能可能無法在 tcsh,fish 或 dash 中使用,同樣的,在 tcsh,fish 或 dash 中能使用的功能也可能無法在 bash 中使用。
檢視你的 bash 命令歷史記錄
history
命令最基本,最頻繁的用法就是檢視你的 shell 會話的命令歷史記錄:
事件提示符$ echo "hello"
hello
$ echo "world"
world
$ history
1 echo "hello"
2 echo "world"
3 history
事件提示符 (!) 是按事件搜尋歷史記錄的。這裡的事件,指的是每一條記錄在歷史記錄裡的命令。換句話說,它就是一行命令,並被數字索引標記著以供引用。
要重新執行歷史記錄中的乙個命令,用 ! 直接加上 (無空格) 你想要執行的命令前面的索引數字即可。例如,假設歷史記錄中的第一條指令是echo hello
,然後你想重新執行它:
你還可以通過從歷史記錄中的當前位置開始提供負數的行來使用相對定位。例如,返回歷史記錄中倒數第3條命令:$ !1
echo "hello"
hello
如果你只想返回上一條命令,你可以使用簡寫 !! 來替代 !-1。這整整節省了一次按鍵的時間!!!$ echo "alvin"
alvin
$ echo "hello"
hello
$ echo "world"
world
$ !-3
echo "alvin"
alvin
字串搜尋$ echo "alvin"
alvin
$ !!
echo "alvin"
alvin
你也可以通過特定的字串來搜尋歷史記錄中的命令並執行它。
若是想要搜尋以特定字串開頭的命令,就用 ! 直接加上 (無空格) 你想要搜尋的字串:
你還可以搜尋在任意位置包含特定字串的命令。要做到這點,你只需要用 ! 直接加上前後兩端都被 ? 包圍的特定字串即可,像這樣:$ echo "alvin"
alvin
$ true
$ false
$ !echo
echo "alvin"
alvin
如果你知道你想要搜尋的字串在命令的最後面,那就可以省略字串後面的 ?,像這樣:$ echo "alvin"
alvin
$ true
$ false
$ !?alvin?
echo "alvin"
alvin
值得注意的是,若是歷史記錄中包含目標字串的命令不止一條,則它只會執行符合條件的命令中最後的一條:$ echo alvin
alvin
$ !?alvin
echo alvin
alvin
字串替換$ echo "hello world"
hello world
$ echo "hello alvin"
hello alvin
$ !?hello?
echo "hello alvin"
hello alvin
你可以搜尋乙個特定的字串並用新字串替換它,從而更改命令:
但是它只能替換第一次出現的目標字串,若是命令**現兩次目標字串,則只有第一次出現的會被替換,像這樣:$ echo "hello"
hello
$ echo "world"
world
$ ^hello^alvin
echo "alvin"
alvin
與字串搜尋一樣,當歷史記錄中包含目標字串的命令不止一條時,只替換並執行最後一條:$ echo "hello hello"
hello hello
$ ^hello^alvin
echo "alvin hello"
alvin hello
充分利用 history 命令$ echo "hello world"
hello world
$ echo "hello"
hello
$ ^hello^alvin
echo "alvin"
alvin
實際上,bash 的history
命令的功能遠不止本文所提到的,但這是你習慣使用history
命令的乙個很好的開始,而不僅僅是利用history
來檢視歷史記錄。要經常使用history
命令,看看你只利用history
命令而不輸入具體的指令能完成多少事情,你會被驚豔到的。
命令記錄 bash history
原貼 http blog.chinaunix.net u 13329 showart.php?id 84767 命令記錄.bash history bash shell在 bash history 表示使用者目錄 檔案中儲存了500條使用過的命令,這樣可以使你輸入使用過的長命令變得容易。每個在系統中...
Shell命令記錄 bash history刪除
bash shell在 bash history 表示使用者目錄 檔案中儲存了500條使用過的命令,這樣可以使你輸入使用過的長命令變得容易。每個在系統中擁有賬號的使用者在他的目錄下都有乙個 bash history 檔案。bash shell應該儲存少量的命令,並且在每次使用者登出時都把這些歷史命令...
Vim 善用 命令
場景二 在 號前後加上空格 場景三 替換字串,但不是需要替換所有出現的字串 更好的閱讀體驗 命令 說明 重複上次的修改 知道思想 對於重複性的修改操作,用一鍵移動,一鍵修改 命令 var a 11 var b 12 var c 13 具體鍵盤操作 a 進入插入模式 delete 刪除最後乙個字元 e...