3

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?

sorin
  • 12,189

2 Answers2

3

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

0

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