指令曆史:shell程序會在其會話中儲存此前使用者送出執行過的指令;
~]#history
定制history的功能,可通過環境變量實作;
HISTSIZE:shell程序可保留的指令曆史的條數;
~]# echo $HISTSIZE
1000
HISTFILE:持久儲存指令曆史的檔案;
.bash_history
~]# cat .bash_history
HISTFILESIZE:指令曆史檔案的大小;
~]# echo $HISTFILE
/root/.bash_history
~]# echo $HISTFILESIZE
指令用法:
history [-c] [-d 偏移量] [n]
或history -anrw [檔案名]
或history -ps 參數 [參數...]
-a:追加本次會話新執行的指令曆史清單至曆史檔案中
-c:清空指令曆史;
-d:删除指定指令曆史
-r:從檔案讀取指令曆史至曆史清單中
-w:把曆史清單中的指令追加至曆史檔案中
history #:顯示最近的#條指令
調用指令曆史清單中的指令:
!#:再一次執行曆史清單中的第#條指令
!!:再一次執行上一條指令:
!STRING:再一次執行指令曆史清單中最近一個以STRING開頭的指令
注意:指令的重複執行有時候需要依賴于幂等性;
調用上一條指令的最後一個參數;
快捷鍵:ESC,.
字元串:!$
控制指令曆史記錄的方式:
環境變量:HISTCONTROL
~]# echo $HISTCONTROL
ignoredups #忽略重複的指令;
ignorespace: 忽略以空格開頭的指令
ignoreboth: 以上兩者同時生效
修改變量的值:
NAME='VALUE'
~]#HISTCONTROL=ignorespace
指令示例:
可保留指令條數:
<code>[root@note1 ~]</code><code># echo $HISTSIZE #保留1000條</code>
<code>1000</code>
曆史檔案位置:
<code>[root@note1 ~]</code><code># echo $HISTFILE #由于是隐藏檔案,可使用ls -a檢視</code>
<code>/root/</code><code>.bash_history</code>
清空history:
<code>[root@note1 ~]</code><code># history -c #清空曆史</code>
<code>[root@note1 ~]</code><code># history #沒有指令了</code>
<code> </code><code>1 </code><code>history</code>
<code>[root@note1 ~]</code><code># cat -n .bash_history #清除的隻是history中記錄.bash_history沒有清空</code>
<code> </code><code>1 </code><code>history</code>
<code> </code><code>2 </code><code>cat</code> <code>.bash_history </code>
<code> </code><code>3 </code><code>history</code> <code>-r</code>
<code>[root@note1 ~]</code><code># history -r #使用-r參數讀書指令曆史(.bash_history)到history中</code>
<code>[root@note1 ~]</code><code># history #再次輸入history,能檢視曆史指令了</code>
<code> </code><code>2 </code><code>history</code> <code>-r</code>
<code> </code><code>3 </code><code>history</code>
<code> </code><code>4 </code><code>cat</code> <code>.bash_history</code>
調用曆史指令清單中的指令:
曆史指令中第292條指令是curl -I www.baidu.com
<code> </code><code>290 </code><code>echo</code> <code>$HISTFILE</code>
<code> </code><code>291 </code><code>echo</code> <code>$HISTFILESIZE</code>
<code> </code><code>292 curl -I www.baidu.com</code>
<code> </code><code>293 </code><code>history</code>
<code>[root@note1 ~]</code><code># !292 #!292表示執行第292條曆史指令</code>
<code>curl -I www.baidu.com</code>
<code>HTTP</code><code>/1</code><code>.1 200 OK</code>
<code>Server: bfe</code><code>/1</code><code>.0.8.14</code>
<code>略</code>
<code>[root@note1 ~]</code><code># !curl #!curl表示執行最近一次帶有curl的指令</code>
調用上一條指令最後一個參數:
<code>[root@note1 ~]</code><code># echo $HISTFILESIZE</code>
<code>[root@note1 ~]</code><code># ls $HISTFILESIZE # $HISTFILESIZE參數是按ESC鍵再按.回車後顯示的</code>
本文轉自cix123 51CTO部落格,原文連結:http://blog.51cto.com/zhaodongwei/1832758,如需轉載請自行聯系原作者