How to Delete History of Last 10 Commands in Shell

How to delete history in a range in linux bash

for ((i=258;i<=262;++i)); do "history -d $i"; done;

as you said, is simply not working due to quotes

for i in {5..10}; do history -d $i; done;

is working, but you must ensure to have entries from 5 to 10, which in your examples, you don't have.

Take into account that every item you remove, the position indexes of the other items scale down of one.

Basically you should reverse loop items:

for i in {10..5}; do history -d $i; done;

to not be affected by the position index changes while looping.

Edit
As Cyrus suggested, there's another approach.

You want to remove items from 258 to 262, a total of 5 entries (262 is included)

You can delete 5 times the entry at position 258.

Taking advantage of the index scaling down of one every time you delete an item:

  • the first time, you delete the item 258
  • the second time, you delete the ex-item 259, that has scaled down to 258
  • the third time, you delete the ex-ex-item 260, that has scaled down to 259 (in the first delete) and to 258 (in the second delete)
  • the fourth time, you delete the ex-ex-ex-item 261, that has scaled down to 260 (in the first delete), to 259 (in the second delete) and to 258 (in the third delete)
  • the fifth time, you delete the ex-ex-ex-ex-item 262...

Print the history for the last 10 Commands on the session on each log in to the shell

Bash history is only loaded from file after .bashrc has completed.

You can force the loading of history and then output what you want by adding the following to your .bashrc file:

history -r
history 10

How to delete a single command from history in bash

So turns out it's solved by deleting the command from ~/bash_history

How to delete entries from fish shell's command history?

You want history delete. That should ask you for a search term, show you matching entries and ask you for which to delete.

Delete last executed command in Linux terminal

To do this you need to save the cursor position before the command and then restore the position after while clearing the rest of the screen.

Something like this should work:

$ hiderun() {
# Move cursor up one line.
tput cuu 1
# Save cursor position.
tput sc
# Execute the given command.
"$@"
# Restore the cursor position.
tput rc
# Clear to the end of the screen.
tput ed
}

$ id
uid=0(root) gid=0(root) groups=0(root)
$ uname -a
Linux debian 3.2.0-4-amd64 #1 SMP Debian 3.2.54-2 x86_64 GNU/Linux
$ tput sc
$ hiderun do something

This probably only works for a single-line prompt. Multiple line prompts probably need to change the argument to tput cuu.

Hm... having your prompt run tput sc as the first thing might mean this Just Works without needing to play any counting/etc. games but that would need some testing.



Related Topics



Leave a reply



Submit