天天看點

history指令解析_學習筆記

時間:2017.11.13

作者:李強

參考:man,info,magedu講義

聲明:以下英文純屬個人翻譯,英文B級,歡迎糾正,盜版不糾,才能有限,希望不誤人子弟為好。

1、使用目的與場景

    實作快速操作指令的一種方式

2、官方說明

    Display or manipulate the history list.

3、寫在前面

    首先這裡有兩個概念history list和history file。下面會使用到,history list就是memory中的東西,history file就是disk上的東西。history list 針對目前會話生效,修改history file是針對所有使用者生效。

當我們通過終端登陸系統後,就會自動将預設history file檔案中的指令添加到history list中去,當然logout後history list中的指令就會自動寫入到history file中去,就是存盤。流程圖就不畫了自己意會。

4、修改檔案及涉及的環境變量

檔案:

~/.bash_history 曆史清單的預設儲存位置
~/.bash_profile  目前使用者的環境變量的設定位置
/etc/profile 系統環境變量的預設設定位置
 /etc/profile.d/env.sh (這個檔案自定義): 手動添加的修改環境變量的設定位置

    系統是啥,系統就是"地主"想做啥做啥。

變量:

HISTSIZE 指令曆史記錄的條數
HISTFILE 指定曆史檔案,預設為~/.bash_history 
HISTFILESIZE 指令曆史檔案記錄曆史的條數 
HISTTIMEFORMAT HISTTIMEFORMAT="%F %T"   顯示時間
HISTIGNORE HISTIGNORE="str1:str2*:… "忽略str1指令,str2開頭的曆史
HISTCONTROL ignoredups 預設,忽略重複的指令,連續且相同為“重複” 
ignorespace 忽略所有以空白開頭的指令
ignoreboth 相當于ignoredups, ignorespace的組合 
erasedups 删除重複指令 

ps.設定的是history list的值,執行history我一次不想看太多指令

ps.和date的format是一樣的。 

ps.可以設定忽略已空格開頭的指令和下面的HISTCONTROL=ignorespace相同,在指令加空格就不會記錄了;比對是精确比對,str1會忽略,但str1xx就會記錄。

ps.可以用 : 隔開,配合設定,HISTCONTROL='ignorespace:ignoredups' 

export 變量名="值“ 存放在 /etc/profile 或 ~/.bash_profile

5、用法(不按字母排序)

history: history [-c] [-d offset] [n] or history -anrw [filename] 
                   or history -ps arg [arg...]
    #ps.官方,以下為個人解釋。
    -c   清空目前history list緩存
    -d   删除list中的某個指令,後面跟的是number号,就是上面的offset
     n   顯示最近n條指令
    -a   append 添加目前會話history list中的指令曆史到檔案中去
    -n   new 隻添加history list中沒有從history file中添加過的指令
    -r   read 讀取history file中的檔案到目前session的history file中
    -w   write 把目前history file中的指令覆寫到history檔案中去。
    可以跟上另外的history file做備份,也可以配合-c先清空然後再預設不加[filename],
    清空~/.bash_history 檔案。
    -p   執行但是不記錄到history list中
    -s   會把參數添加到history list中去,但是并不執行      

以上是option選項,以下還有一些bash對history list的快捷操作 History expansions

    The History library provides a history expansion feature that is similar to the history expansion provided by `csh'.  This section describes the syntax used to manipulate the history information.

    History expansions introduce words from the history list into the input stream, making it easy to repeat commands, insert the arguments to a previous command into the current input line, or fix errors in previous commands quickly.

    History expansion takes place in two parts.  The first is todetermine which line from the history list should be used during substitution.  The second is to select portions of that line for inclusion into the current one.  The line selected from the history iscalled the "event", and the portions of that line that are acted upon are called "words".  Various "modifiers" are available to manipulate the selected words.  The line is broken into words in the same fashion that Bash does, so that several words surrounded by quotes are considered one word.  

    History expansions are introduced by the appearance of the history expansion character, which is `!' by default

以下為本人翻譯,湊活着看:

    history調用将history list 清單中的單詞引入輸入流, 使重複指令變得容易, 将參數插入到目前輸入行中的上一個指令, 或快速修複以前指令中的錯誤。

    history調用分成2個部分,第一我們要明确我們在置換的時候選擇的是history list的哪一行,第二選擇這行的哪個部分去加入到目前指令中。 行的選擇我們稱為”event“,選擇這行的哪一部分稱為‘’words‘’

history的調用預設用!字元引入調用
執行操作格式如下:
   [command]  !xx:yy   !xx哪一行指令,中間":"分開,yy 第幾個參數
event Designators辨別符,以下指定哪一行被調用。
`!'     :開始history調用,除非後面跟spasce,tab,或者`='在後面,就不是這個意思了,
#ps.後面要直接加東西的意思,如下面所示必要手殘多敲些東西的意思
`!N'     :執行第N行指令
`!-N'    :執行倒數第N行指令
`!!'     :執行剛才或者上一個執行的指令,相當于 `!-1'  
`!STRING'  :從最後一個往前,執行最近的一個以STRING開頭的指令
`!?STRING[?]':從最後一個往前,執行最近的一個包含STRING開頭的指令
#這裡有個快捷鍵實作上面2個方式,Ctrl+r向上查找最近的一個以某個字元開頭或
包含某個字元的指令回車執行,Ctrl +g 取消搜尋
`^STRING`:替換上一條指令的第一個STRING
`^STRING1^STRING2^':快速替換,将上條指令中的STRING1替換成STRING2
!!:$'    :辨別前一個指令的最後一個參數可以縮寫成 `!$'
`!fi:2'   :表示最近一個以fi開始指令的第二個參數,表示不懂
#以下是一些word Designators辨別符
`0 (zero)' :第0個單詞,對于大部分應用來說是指令本身
#ps. "!:0" 執行前一個指令但是不加參數。
`N'     :第N個單詞
`^'     :第一個參數,也是第一個單詞
`$'     :最後一個參數
`%'
The word matched by the most recent `?STRING?' search.
`X-Y'   一個單詞的範圍,`-Y' 是 `0-Y'的縮寫
`*'     :所有的單詞,除了第0個也就是指令本身和`1-$'是相同的意思
`X*'     :`X-$'的縮寫
`X-
modify
After the optional word designator, you can add a sequence of one or
more of the following modifiers, each preceded by a `:'.
    
`h'     :移除尾部的路徑,保留路徑的頭
`t'     :移除路徑的頭部,保留尾,
e.g.
[lq@centos6 ~]$ll /etc/passwd /etc/motd
-rw-r--r--. 1 root root    0 Jan 12  2010 /etc/motd
-rw-r--r--. 1 root root 1920 Nov 15 19:02 /etc/passwd
[lq@centos6 ~]$echo "!!:t"
echo "motd"
motd
`r'     :移除`.SUFFIX'字尾,保留檔案名
e.g.
[root@centos6 /app]#ll  1.txt 2.txt        
-rw-r--r--. 1 root root 0 Nov 15 22:27 1.txt
-rw-r--r--. 1 root root 0 Nov 15 22:27 2.txt
[root@centos6 /app]#echo "!!:r"            
echo "ll  1.txt 2"
ll  1.txt 2
`e'     :移除所有,保留`.SUFFIX'字尾
e.g.
[root@centos6 /app]#ll  1.txt 2.txt    
-rw-r--r--. 1 root root 0 Nov 15 22:27 1.txt
-rw-r--r--. 1 root root 0 Nov 15 22:27 2.txt
[root@centos6 /app]#echo "!!:e"
echo ".txt "
.txt 
`p'     :列印指令但是不執行
e.g.
[root@centos6 /app]#!518:p
rm -rf *
[root@centos6 /app]#!-10:p
ll /etc/passwd 
`s/OLD/NEW/'
     Substitute NEW for the first occurrence of OLD in the event line.
     Any delimiter may be used in place of `/'.  The delimiter may be
     quoted in OLD and NEW with a single backslash.  If `&' appears in
     NEW, it is replaced by OLD.  A single backslash will quote the
     `&'.  The final delimiter is optional if it is the last character
     on the input line.     
`&'        :重複之前的替換,暫時沒找到使用方法
`g'
#ps.雖然看不懂但是是怎麼用的 
#!:gs/string1/string2 将上一條指令中所有的string1都替換為string2
`a'
     Cause changes to be applied over the entire event line.  Used in
     conjunction with `s', as in `gs/OLD/NEW/', or with `&'.
     `G'
     Apply the following `s' modifier once to each word in the event.      

6、實際使用:

    command !^     : 利用上一個指令的第一個參數做cmd的參數 

    command !$      : 利用上一個指令的最後一個參數做cmd的參數 

    command !*      : 利用上一個指令的全部參數做cmd的參數 

    command !:n     : 利用上一個指令的第n個參數做cmd的參數

    ps. !^  可以了解為"!-1:^"或者"!!:^"的縮寫

         !$! 可以了解為"!-1:$"或者"!!:$"的縮寫

         !*! 可以了解為"!-1:*"  或者"!!:*"的縮寫

        這裡就可以了解了!!在加上:之後可以縮寫為!,這就是之前的!:0代表上一個指令的意思。

        下面的就好了解了!xx:yy

    command !n:^      調用第n條指令的第一個參數 

    command !n:$      調用第n條指令的最後一個參數 

    command !n:n     調用第n條指令的第n個參數 

    command !n:*       調用第n條指令的所有參數

    command !string:^     從指令曆史中搜尋以 string 開頭的指令 ,并擷取它的第一個參數 

    command !string:$     從指令曆史中搜尋以 string 開頭的指令 ,并擷取它的最後一個參數

    command !string:n     從指令曆史中搜尋以 string 開頭的指令 ,并擷取它的第n個參數 

    command !string:*     從指令曆史中搜尋以 string 開頭的指令 ,并擷取它的所有參數

湊合看吧,順便說下我不是喜歡綠色,我喜歡的是大自然的顔色

history指令解析_學習筆記