Is there a way to get a command from history without executing it?
Maybe I’m missing the point of the question, as I doubt that I’m the only person who knows about
:p. It stands for “print”. If you type
!100:p, the shell will print (i.e., display) command #100 without executing it.
If you don’t know about the
: operators in history, you should learn.
As “numeric illustration” points out,
:n retrieves the n th word of a command.
You can also use
:n-m to get a range;
and there are interesting defaults.
:- (leaving n and m blank) gets you all the words except for the last one.
:$ gets you the last word.
:h is “head”, so if you type
ls -ld /asdfg/qwerty/foo/bar
ls –ld /asdfg/qwerty/foo
!$ is short for
:s is substitute, as in
This is the same as
^old^new^ except for things like
:s can be used on any command (as in
^^ works only on the most recent command.
:s can be used on individual words, or ranges of words. For example,
ls -ld /asDfg/qwerty/foo/bar
ls -lD ...).
:s can be applied globally. The syntax is non-intuitive, but, where
:s/old/new/ replaces the first occurrence,
:gs/old/new/ replaces every occurrence.
Also, once you've fetched a command from history without executing it
!100:p), possibly with modifications (as shown above),
it becomes the most recent command in your history list.
Now you can press ↑ (cursor up) and edit it.