How to delete history of last 10 commands in shell?

前端 未结 19 1845
孤城傲影
孤城傲影 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:49

    With Bash 5 you can now do a range...Hooray!:

    history -d 511-520
    

    or counting backwards 10:

    history -d -10--1
    

    Excerpt from Bash 5 Man Page:

    'history'

    Options, if supplied, have the following meanings:

    '-d OFFSET' Delete the history entry at position OFFSET. If OFFSET is positive, it should be specified as it appears when the history is displayed. If OFFSET is negative, it is interpreted as relative to one greater than the last history position, so negative indices count back from the end of the history, and an index of '-1' refers to the current 'history -d' command.

    '-d START-END' Delete the history entries between positions START and END, inclusive. Positive and negative values for START and END are interpreted as described above.

    Here is my solution for Bash 4. It iteratively deletes a single entry or a range of history starting with lowest index.

    delHistory () {
        count=$(( ${2:-$1} - $1 ))
        while [[ $count -ge 0 ]]; do
            history -d "$1"
            ((count--))
        done
    }
    
    delHistory 511 520
    

提交回复
热议问题