How to delete history of last 10 commands in shell?

前端 未结 19 2182
孤城傲影
孤城傲影 2021-01-29 18:47

Commands follows

  511  clear
  512  history
  513  history -d 505
  514  history
  515  history -d 507 510 513
  516  history
  517  history -d 509
  518  hist         


        
19条回答
  •  太阳男子
    2021-01-29 19:48

    I use this (I have bash 4):

    histrm() {
        local num=${1:- 1}
        builtin history -d $(builtin history | sed -rn '$s/^[^[:digit:]]+([[:digit:]]+)[^[:digit:]].*$/\1/p')
        (( num-- )) && $FUNCNAME $num
        builtin history -w
    }
    

    The builtin history parts as well as the last -w is because I have in place a variation of the famous tricks to share history across terminals and this function would break without those parts. They ensure a call to the real bash history builtin (and not to my own wrapper around it), and to write the history to HISTFILE right after the entries were removed.

    However this will work as it is with "normal" history configurations.

    You should call it with the number of last entries you want to remove, for example:

    histrm 10
    

    Will remove the last 10 entries.

提交回复
热议问题