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.

How can I get the last executed command from bash? I know that !!:p prints the last command, but it seems I can't use that anywhere except the bash prompt. I tried echo !!:p but it prints

~/Downloads$ pwd
Downloads
~/Downloads$ echo !!:p
echo pwd

I want to use this inside the PROMPT_COMMAND variable so I need to get it as a string so I can just print it out. Is there an easy way to do this? Am I looking in the wrong place?

I guess I'm not clear. I need to store the last command run so I can re-display it after the output and before the next prompt. For example this is what I want the output to look like:

~/Downloads$ pwd
Downloads

pwd
~/Downloads$

I'm doing this my changing my prompt in my .bashrc file

PROMPT_COMMAND='echo -en "\033[38;5;2m"!!:p"\033[0m\n"'
PS1='\W\$'

But !!:p only works correctly from the bash prompt. So how can I store the last command so that I can reprint it later?

share|improve this question
    
It's not clear what you mean by 'Printing(saving)'. Where do you want the command saved to? –  conorgriffin Oct 15 '10 at 23:31

1 Answer 1

up vote 4 down vote accepted

You can access the just-executed command line with the history built-in. (I have no idea why history 1 prints the just-executed command line but fc -nl -1 prints the previous commmand, as does fc -nl 0.)

PROMPT_COMMAND='echo -en "\033[38;5;2m"; history 1; echo -en "\033[0m\n"'

This prints a number before the command text. Here's a version that removes the number. (It may be incorrect if you go beyond 99999 history lines, I don't know how bash formats the number then.)

prompt_function () {
  local prompt_history="$(history 1)"
  prompt_history=${prompt_history:7}
  echo -En $'\033[38;5;2m'"$prompt_history"$'\033[0m\n'
}
PROMPT_COMMAND=prompt_function

(Note that echo -en ..."$prompt_history"... would expand backslashes in the command line, so I use echo -E and let the shell expansion generate the control characters with $''.).

share|improve this answer
    
Cool, thanks. I can't say I understand 100% of why exactly that works, but it does =] –  Falmarri Oct 16 '10 at 0:26
    
the $'\033[38;5;2m' and $'\033[0m\n' parts are ANSI escape codes. The first one colors the contents of $prompt_history and the second resets the color to your terminals default. –  conorgriffin Oct 16 '10 at 0:50
    
I guess I should have clarified. What exactly is the reason for having to store prompt_history twice? Why does it need first the $() and then the {:7}? I think it has to do with executing the command every time instead of caching, but what are the rules on that? Is that why it's written how it is? –  Falmarri Oct 16 '10 at 8:45
    
@Falmarri: history 1 prints a number before the command. As far as I know you can't directly perform string manipulation like ${:7} on the result of a command substitution in bash, hence the temporary variable. See my revised answer. –  Gilles Oct 16 '10 at 9:49
    
Sorry one more question. I'm not an expert in bash (yet), can you quickly explain the quotes in your echo? For example why is the first $ outside the quotes, and why is the ascii control character in single quotes? –  Falmarri Oct 16 '10 at 19:28

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.