Skip to main content
use of 'tac' assumes linux, this question pertains to all bash terminals
Source Link
for h in $(seq 1006 1008 | tac); do history -d $h;1006; done

This will generate history -d commands for 10081006, then 1007 becomes 1006 and 1006 is deleted, then 1008 (became 1007) is now 1006 and gets deleted.

for h in $(seq 1006 1008 | tac); do history -d $h;1006; done; history -d $(history 1 | awk '{print $1}')
histdel(){
  for h in $(seq $1 $2 | tac); do
    history -d $h$1
  done
  history -d $(history 1 | awk '{print $1}')
  }
for h in $(seq 1006 1008 | tac); do history -d $h; done

This will generate history -d commands for 1008 then 1007 then 1006.

for h in $(seq 1006 1008 | tac); do history -d $h; done; history -d $(history 1 | awk '{print $1}')
histdel(){
  for h in $(seq $1 $2 | tac); do
    history -d $h
  done
  history -d $(history 1 | awk '{print $1}')
  }
for h in $(seq 1006 1008); do history -d 1006; done

This will generate history -d commands for 1006, then 1007 becomes 1006 and 1006 is deleted, then 1008 (became 1007) is now 1006 and gets deleted.

for h in $(seq 1006 1008); do history -d 1006; done; history -d $(history 1 | awk '{print $1}')
histdel(){
  for h in $(seq $1 $2); do
    history -d $1
  done
  history -d $(history 1 | awk '{print $1}')
  }
Updated to use simpler commands.
Source Link
JGC
  • 6.2k
  • 2
  • 34
  • 32
history | tail -1 | awk '{print $1}'
for h in $(seq 1006 1008 | tac); do history -d $h; done; history -d $(history | tail -1 | awk '{print $1}')
histdel(){
  for h in $(seq $1 $2 | tac); do
    history -d $h
  done
  history -d $(history | tail -1 | awk '{print $1}')
  }
[18:21:02 jonathag@gb-slo-svb-0221 ~]$ history | tail -n 11
 1046  25-04-2016 18:20:47 echo "Command 1"
 1047  25-04-2016 18:20:48 echo "Command 2"
 1048  25-04-2016 18:20:50 echo "Command 3"
 1049  25-04-2016 18:20:51 echo "Command 4"
 1050  25-04-2016 18:20:53 echo "Command 5"
 1051  25-04-2016 18:20:54 echo "Command 6"
 1052  25-04-2016 18:20:56 echo "Command 7"
 1053  25-04-2016 18:20:57 echo "Command 8"
 1054  25-04-2016 18:21:00 echo "Command 9"
 1055  25-04-2016 18:21:02 echo "Command 10"
 1056  25-04-2016 18:21:07 history | tail -n 11
[18:21:07 jonathag@gb-slo-svb-0221 ~]$ histdel 1049 1051
[18:21:23 jonathag@gb-slo-svb-0221 ~]$ history | tail -n 8
 1046  25-04-2016 18:20:47 echo "Command 1"
 1047  25-04-2016 18:20:48 echo "Command 2"
 1048  25-04-2016 18:20:50 echo "Command 3"
 1049  25-04-2016 18:20:56 echo "Command 7"
 1050  25-04-2016 18:20:57 echo "Command 8"
 1051  25-04-2016 18:21:00 echo "Command 9"
 1052  25-04-2016 18:21:02 echo "Command 10"
 1053  25-04-2016 18:21:07 history | tail -n 11
histdeln(){

  # Get the current history number
  n=$(history | tail -1 | awk '{print $1}')

  # Call histdel with the appropriate range
  histdel $(( $n - $1 )) $(( $n - 1 ))
  }
history | tail -1 | awk '{print $1}'
for h in $(seq 1006 1008 | tac); do history -d $h; done; history -d $(history | tail -1 | awk '{print $1}')
histdel(){
  for h in $(seq $1 $2 | tac); do
    history -d $h
  done
  history -d $(history | tail -1 | awk '{print $1}')
  }
[18:21:02 jonathag@gb-slo-svb-0221 ~]$ history | tail -n 11
 1046  25-04-2016 18:20:47 echo "Command 1"
 1047  25-04-2016 18:20:48 echo "Command 2"
 1048  25-04-2016 18:20:50 echo "Command 3"
 1049  25-04-2016 18:20:51 echo "Command 4"
 1050  25-04-2016 18:20:53 echo "Command 5"
 1051  25-04-2016 18:20:54 echo "Command 6"
 1052  25-04-2016 18:20:56 echo "Command 7"
 1053  25-04-2016 18:20:57 echo "Command 8"
 1054  25-04-2016 18:21:00 echo "Command 9"
 1055  25-04-2016 18:21:02 echo "Command 10"
 1056  25-04-2016 18:21:07 history | tail -n 11
[18:21:07 jonathag@gb-slo-svb-0221 ~]$ histdel 1049 1051
[18:21:23 jonathag@gb-slo-svb-0221 ~]$ history | tail -n 8
 1046  25-04-2016 18:20:47 echo "Command 1"
 1047  25-04-2016 18:20:48 echo "Command 2"
 1048  25-04-2016 18:20:50 echo "Command 3"
 1049  25-04-2016 18:20:56 echo "Command 7"
 1050  25-04-2016 18:20:57 echo "Command 8"
 1051  25-04-2016 18:21:00 echo "Command 9"
 1052  25-04-2016 18:21:02 echo "Command 10"
 1053  25-04-2016 18:21:07 history | tail -n 11
histdeln(){

  # Get the current history number
  n=$(history | tail -1 | awk '{print $1}')

  # Call histdel with the appropriate range
  histdel $(( $n - $1 )) $(( $n - 1 ))
  }
history 1 | awk '{print $1}'
for h in $(seq 1006 1008 | tac); do history -d $h; done; history -d $(history 1 | awk '{print $1}')
histdel(){
  for h in $(seq $1 $2 | tac); do
    history -d $h
  done
  history -d $(history 1 | awk '{print $1}')
  }
[18:21:02 jonathag@gb-slo-svb-0221 ~]$ history 11
 1046  25-04-2016 18:20:47 echo "Command 1"
 1047  25-04-2016 18:20:48 echo "Command 2"
 1048  25-04-2016 18:20:50 echo "Command 3"
 1049  25-04-2016 18:20:51 echo "Command 4"
 1050  25-04-2016 18:20:53 echo "Command 5"
 1051  25-04-2016 18:20:54 echo "Command 6"
 1052  25-04-2016 18:20:56 echo "Command 7"
 1053  25-04-2016 18:20:57 echo "Command 8"
 1054  25-04-2016 18:21:00 echo "Command 9"
 1055  25-04-2016 18:21:02 echo "Command 10"
 1056  25-04-2016 18:21:07 history 11
[18:21:07 jonathag@gb-slo-svb-0221 ~]$ histdel 1049 1051
[18:21:23 jonathag@gb-slo-svb-0221 ~]$ history 8
 1046  25-04-2016 18:20:47 echo "Command 1"
 1047  25-04-2016 18:20:48 echo "Command 2"
 1048  25-04-2016 18:20:50 echo "Command 3"
 1049  25-04-2016 18:20:56 echo "Command 7"
 1050  25-04-2016 18:20:57 echo "Command 8"
 1051  25-04-2016 18:21:00 echo "Command 9"
 1052  25-04-2016 18:21:02 echo "Command 10"
 1053  25-04-2016 18:21:07 history 11
histdeln(){

  # Get the current history number
  n=$(history 1 | awk '{print $1}')

  # Call histdel with the appropriate range
  histdel $(( $n - $1 )) $(( $n - 1 ))
  }
Tailored the final portion to the exact question.
Source Link
JGC
  • 6.2k
  • 2
  • 34
  • 32

The question was actually to delete the last 10 commands from history, so if you want to save a little effort you could use another function to call the histdel function which does the calculations for you.

histdeln(){

  # Get the current history number
  n=$(history | tail -1 | awk '{print $1}')

  # Call histdel with the appropriate range
  histdel $(( $n - $1 )) $(( $n - 1 ))
  }

This function takes 1 argument, the number of previous history items to delete. So to delete the last 10 commands from history just use histdeln 10.

The question was actually to delete the last 10 commands from history, so if you want to save a little effort you could use another function to call the histdel function which does the calculations for you.

histdeln(){

  # Get the current history number
  n=$(history | tail -1 | awk '{print $1}')

  # Call histdel with the appropriate range
  histdel $(( $n - $1 )) $(( $n - 1 ))
  }

This function takes 1 argument, the number of previous history items to delete. So to delete the last 10 commands from history just use histdeln 10.

Source Link
JGC
  • 6.2k
  • 2
  • 34
  • 32
Loading