Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

You probably encuntered the case where a package was not installed and the system was recommending you to install it using apt-get install mypackage.

I am looking for a quick keyboard shortcut for executing this last line. Obviously UP arrow would not bring it because it was not a command executed by you and only the last output line of the previous executed program.

Any ideas on how to get this behaviour in bash?

share|improve this question
By default the output isn't "stored" anywhere. You might be able to hack together a solution… see here: automatically capture output of last executed command into a variable – slhck Nov 23 '13 at 8:04
what's wrong with copy and paste? – MariusMatutiae Nov 23 '13 at 11:04
this is not an easy thing to accomplish.... – DragonZero Nov 23 '13 at 13:56
@MariusMatutiae that's what I did for years, but it requires me to switch from keys to pointing device select, copy, click, paste, clearly a "complex" process. Maybe I will endup implementing a shortcut my in the terminal (iTerm2). – sorin Nov 24 '13 at 15:36

You can make an alias xo (name it whatever you like) that reruns the last command without printing any of its output, then runs the last line of that output as a command.

alias xo='$($(fc -ln -1) |& tail -1)'

Then after you attempt to run myapp and are told the command you can use to install mypackage which provides myapp, you can simply type xo and press Enter and the command to install mypackage will run.

How This Works

$(fc -ln -1) behaves much like the history expansion !!. !! doesn't work in an alias or function definition, but fc (and command substitution with $( )) does.

$(fc -ln -1) |& tail -1 reruns the last command, piping its standard output and standard error to tail -1, which discards everything but the last line.

Enclosing that in an outer command substitution causes the last line to be run as a command. Word splitting is performed on it (since the whole thing is not enclosed in " quotes), which is what you want.

Sources Cited

share|improve this answer

Possible solution would be $(!! |& tail -1)as it says in this topic:

share|improve this answer
Your now-deleted answer to the related Ask Ubuntu question looked potentially quite useful. Provided the utility you recommended really does have this particular feature, I suggest editing your answer for clarification and undeleting it. You needn't worry about the downvotes--once the post clearly shows that the method works and how to use it, they might be reversed--and even if not, it'll get at least my upvote, and probably others. – Eliah Kagan May 9 '15 at 23:22
@EliahKagan : I can't tell you how many times I've banged the keyboard with <up><home><s><u><d><o>< ><enter>, assuming I don't return again after an annoyed typo. (It's worse on OSX) That utility has fewer keystrokes, reflects an appropriate sentiment and would get my vote. – ǝɲǝɲbρɯͽ May 10 '15 at 0:03
@EliahKagan I've deleted my comment because I'm currently without any Ubuntu distribution to show how it works. I used to have that app in the past and it was working fine but I can't demonstrated right now how it works on Ubuntu that's why the answer was short and I couldn't copy/paste how it works. Feel free to try it and add the answer again. For instance, as you said it wasn't spam I don't event know the owner of the project... Thanks for your help – Gerard Brull May 10 '15 at 9:22

You must log in to answer this question.

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