Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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.... falunk.com/?p=12 –  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

2 Answers 2

Possible solution would be $(!! |& tail -1)as it says in this topic: http://askubuntu.com/questions/621681/how-can-i-execute-the-last-line-of-output-in-bash/621684

share|improve this answer
1  
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 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 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 at 9:22

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

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.