On bash commandline environment, you can do
!! to rerun your last command. And you can press up arrow to see the the last command you ran. But what if you want to only see but not run the last command you ran that began in a certain way?
If you search history via Ctrl+r and typing some letters of the command and not pressing Enter but pressing →, the command will show up and will not run. Another alternative is:
history | grep 'mycommand'
This nice history cheatsheet might also help.
Every answer here, as far as I can tell, is non-portable. For a portable option, consider the POSIX-guaranteed
$ man fc
−e editor Use the editor named by editor to edit the commands. The editor string is a utility name, subject to search via the PATH variable ... The value in the FCEDIT variable shall be used as a default when −e is not specified. If FCEDIT is null or unset, ed shall be used as the editor.
−l (The letter ell.) List the commands rather than invoking an editor on them. The commands shall be written in the sequence indicated by the first and last operands, as affected by −r, with each command preceded by the command number.
−n Suppress command numbers when listing with −l.
−r Reverse the order of the commands listed (with −l) or edited (with neither −l nor −s).
−s Re-execute the command without invoking an editor.
first, last Select the commands to list or edit. The number of previous commands that can be accessed shall be determined by the value of the HISTSIZE variable. The value of first or last or both shall be one of the following:
[+or-]number A positive (or negative) number representing a command number; command numbers can be displayed with the −l option... For example, −1 is the immediately previous command...
When the −l option is used to list commands, the format of each command in the list shall be as follows:
"%d\t%s\n", <line number>, <command>
If both the −l and −n options are specified, the format of each command shall be:
If the consists of more than one line, the lines after the first shall be displayed as:
But be sure to use -l or -e if you ONLY want to see/edit your commands. By default
fc will open the command-list requested in FCEDIT (note that is distinct from your EDITOR environment variable) and, when FCEDIT quits,
fc will run the edited commands.
Anyway, specifically the answer to this question could be:
% fc -l -1
Or with no line numbers:
% fc -ln -1
Or the last five commands in reverse order:
% fc -lrn -1 -5
In your pager:
% fc -lrn -1 -5 |$PAGER
For your last call to
% fc -l fc
If you just want the last command use:
history | cut -c 8- | tail -n 2 | head -n 1
If you want to be able to do that repeatedly, put a space before
history | cut -c 8- | grep -Ev '^history' | tail -n 1
If you just want to see the last command starting with pattern
history | cut -c 8- | grep -E '^xyz' | tail -n 1
But this doesn't work if the pattern would be the initial characters of 'history' and then you would need to something like:
history | cut -c 8- | grep -Ev 'history \|' | grep -E '^hist' | tail -n 1
Add these lines in a file
"\e[A": history-search-backward "\e[B": history-search-forward
source ~/.inputrc in your shell (or just restart shell).
Now you can type a few letters of the command, and then use up/down arrows to cycle through commands which start with those few letters.