!-n
execute command "-n" of your history, but I wouldn't recommend it, as it is "instant" and will not give you time to check first that the "Nth" command is the right one you need to execute again... ( @bac0n nicely remarked that you can set : shopt -s histverify
to switch it to "non instant": it will let you verify the command !-n expands to, before executing it with <Enter>
)
However, if you are using bash
as your shell, I much prefer to recommend using the search feature:
Ctrl and then type something
, which is the part of the command that you are looking for. (ex: Ctrl + r + (type into terminal) echo
)
It will present the latest occurrence in your history matching something
(i.e., matching the search term that you typed after Ctrl + r).
Subsequent Ctrl + r will look further back in your history. Or adding letters will precise the search.
And once you find a good match, press Enter to execute it again (or Ctrl + c to exit from the search without executing the currently displayed matching command).
Additional tip: if, instead of Enter , you press Ctrl + o , it will execute the selected history command AND present the next one, ready to be executed with Enter (which only execute that one and returns to the normal prompt) or Ctrl + o again (which executes it and present the next command in your history). Very useful to repeat a sequence of steps, and providing a display of those commands before executing them.
And each time you use those things, you are allowed to change something on the currently displayed line (using the left or right arrow key to move within it), such as a parameter, a server name, etc., and execute that modified line + go to the next one (if you pressed Ctrl + o).