When I am running my analyses using the bash shell, I often want to save the commands I've used that gave me good results to a file in the same directory (my "LOGBOOK", as its called) so that I can check what I did to get those results. So far this has meant me either copy.pasting the command from the terminal or pressing "up" modifying the command to an echo"my command" >> LOGBOOK, or other similar antics.

I found there was a history tool the other day, but I can't find a way of using it to get the previously executed command so that I can do something like getlast >> LOGBOOK.

Is there a nice easy way to do this. Alternatively, how do others deal with saving the commands for results they have?

5 Answers 5


If you are using bash, you can use the fc command to display your history in the way you want:

fc -ln -1

That will print out your last command. -l means list, -n means not to prefix lines with command numbers and -1 says to show just the last command. If the whitespace at the start of the line (only the first line on multi-line commands) is bothersome, you can get rid of that easily enough with sed. Make that into a shell function, and you have a solution as requested (getlast >> LOGBOOK):

getlast() {
    fc -ln "$1" "$1" | sed '1s/^[[:space:]]*//'

That should function as you have asked in your question.

I have added a slight variation by adding "$1" "$1" to the fc command. This will allow you to say, for example, getlast mycommand to print out the last command line invoking mycommand, so if you forgot to save before running another command, you can still easily save the last instance of a command. If you do not pass an argument to getlast (i.e. invoke fc as fc -ln "" "", it prints out just the last command only).

[Note: Answer edited to account for @Bram's comment and the issue mentioned in @glenn jackman's answer.]

  • 5
    The first form fc -lnr | head -n 1 can be abbreviated to fc -lnr -1.
    – Bram
    Commented May 7, 2012 at 11:45
  • @Bram: Good stuff. I was using help fc for my documentation. The bash man page has more details, including this use of negative indices. Eliminating head is good because the one command left is built-in so now there's no fork/exec.
    – camh
    Commented May 7, 2012 at 11:59
  • 1
    I wonder why the command is called fc... Commented May 7, 2012 at 21:46
  • 2
    It stands for "fix command". gnu.org/software/bash/manual/bashref.html#Bash-History-Builtins
    – Mikel
    Commented May 8, 2012 at 1:15

One problem with @camh's answer is if you have a command that spans multiple lines, it only shows the first line:

$ echo "one
> two
> three"

$ fc -lnr | head -1
         echo "one

Try this:

$ alias getlast='fc -nl $((HISTCMD - 1))'

$ echo "one
> two
> three"

$ getlast
         echo "one
  • 3
    enabling cmdhist will fix that: "If set, bash attempts to save all lines of a multiple-line command in the same history entry. This allows easy re-editing of multi-line commands." Commented May 7, 2012 at 21:17

Instead of using the up arrow, you can use "!!" to refer to the previous command.


$ some -long --command --difficulty="very hard to remember"
$ echo "!!" >> LOGBOOK

note: this does not quote the literal text

  • 6
    That will not capture the literal command. If there are any shell metacharacters, they will be evaluated and substituted. Pipelines will also break: ls | less -> echo !! >> LOGBOOK -> echo ls | less >> LOGBOOK.
    – camh
    Commented May 8, 2012 at 1:04

You can do that with the following command if you are using bash shell:

$> history -a LOGBOOK

This will append all your commands from current session to that file, you can also add an alias on your ~/.bashrc file to do this with a single command:

$> alias getlast="history -a LOGBOOK"
  • 1
    But you're missing the more important requirement that I only save the commands that worked and got the results I have. I don't want to save all the recent commands I've done, just select ones after I've used them. Thanks anyway.
    – MattLBeck
    Commented May 7, 2012 at 10:27
fc -l | awk 'FNR == 15 {print $2}' > /tmp/last_command

I capture the last command with awk from fc -l output and print it to /tmp/last_command


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .