Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
1  
Could you please modify your question. I is still not clear what you are asking. –  user55518 Mar 11 at 18:14

7 Answers 7

up vote 17 down vote accepted

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.

share|improve this answer
1  
And if you are not looking for the first match and want to keep searching backwards, press ctrl-r again and again. And this is only one of many powerful commands provided by bash history & readline library. –  orion Mar 11 at 19:57
    
Considering you have the verified answer, I'll pull mine and if you want to use the "^.\{7\}pattern" expression in your answer, you're welcome to do so! –  Amphiteót Mar 12 at 1:49
    
if you want to edit several command and rerun them you can also use fc ### ### –  Kiwy Mar 12 at 15:28

You can show last command run, by append :p to !!:

$ echo 123
123
$ !!:p
echo 123

If you want show last command run with certain name, use one exclamation:

$ echo 123
123
$ pwd
/home/cuonglm
$ !echo:p
echo 123
share|improve this answer

Every answer here, as far as I can tell, is non-portable. For a portable option, consider the POSIX-guaranteed fc:

    $ 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.

OPERANDS

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:

    "\t%s\n", <command>

If the consists of more than one line, the lines after the first shall be displayed as:

    "\t%s\n", <continued-command>

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:

    % fc -l fc
share|improve this answer
    
+1 for effort. However, the question is tagged bash –  grebneke Mar 12 at 9:55
    
It works in bash, and everything else. The use of bash doesn't preclude portability, but certainly developing bad habits will. –  mikeserv Mar 12 at 10:06

hit Ctrlr then start typing the command you want to see. bash will incrementally complete. When you are satisfied, hit Enter to re-run it or Ctrlg to abort.

share|improve this answer

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 or use:

 history | cut -c 8- | grep -Ev '^history' | tail -n 1

If you just want to see the last command starting with pattern xyz:

 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 
share|improve this answer
    
There's a typo in your first use of cut. –  jon Mar 12 at 0:50
    
@jon Fixed, thanks –  Anthon Mar 12 at 3:40
bind '"\ep": history-search-backward'
bind '"\en": history-search-forward'

And then you can enter some text, and then Alt-p to search for the last commands that start the same (and Alt-p, Alt-n to cycle back and forth trough them).

(Those are the default key binding for similar widgets in zsh)

share|improve this answer

Add these lines in a file ~/.inputrc:

"\e[A": history-search-backward
"\e[B": history-search-forward

Then 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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.