If you were trying to run a package that is not installed, for example:

me ~: gparted 

The program 'gparted' is currently not installed. You can install it by typing:
sudo apt-get install gparted

How can I run the sudo apt-get install gparted line as a command? Not typing it, obviously.

  • Well, you can copy and paste it, but that's still "typing" it at the command prompt. Depending on your terminal, you might be able to select that line with the mouse (try double or triple click) and paste without physically touching the keyboard. – Greg Hewgill Nov 6 '13 at 21:18
  • Well, but if everything is a file in Linux, shouldn't we be able to access it somehow? Anyway, my present workaround is alias ins=sudo apt-get install !! If I find a package that it's not there, then I just type ins. – Quora Feans Nov 6 '13 at 23:13
  • The concept of "everything is a file" does not apply to program output (program output isn't stored anywhere). I suppose you could redirect the output to a file, then use tail -1 to select the last line, and execute that, but that seems like a roundabout way to complete a simple interactive task. – Greg Hewgill Nov 6 '13 at 23:15

You can enable it by adding this to your .bashrc


Giving you:

$ foo
The program 'foo' is currently not installed. You can install it by typing:
sudo apt-get install blah-blah
Do you want to install it? (N/y)

If you get a python error as in:

  File "/usr/lib/python3/dist-packages/CommandNotFound/CommandNotFound.py", line 217, in install_prompt
    answer = answer.decode(sys.stdin.encoding)
AttributeError: 'str' object has no attribute 'decode'

You can:

Apply this patch for Ubuntu from here.
Modify CommandNotFound.py by adding four spaces at beginning of lines 215,216,217 (note: not tabs):

    213            else:
    214                answer = raw_input(prompt)
    215 4 spaces   if sys.stdin.encoding and isinstance(answer, str):
    216 4 spaces       # Decode the answer so that we get an unicode value
    217 4 spaces       answer = answer.decode(sys.stdin.encoding)
    218            if answer.lower() == _("y"):

Another way, to expand on Greg Hewgill's comment could be:

$(!! 2>&1 | tail -n1)

I tried to wrap it in a script, but that didn't work out of the box as you won't get the error message.

If one want to get it in a script, a rather hacky way could be:

x=$(/usr/lib/command-not-found $(tail -n1 "$HOME/.bash_history") 2>&1 | tail -n1)
echo "$x"

For some options look at:

/usr/lib/command-not-found -help

an/or check out the script itself.


There is no way around this to my knowledge. You have 2 options:

  1. type it
  2. copy and paste it (which is technically typing it)


As a 3rd approach you can use the tool auto-apt.

   $ auto-apt run <command_string>

When invoked, the auto-apt command automatically installs packages upon missing file access. If a program tries to access a file known to belong in an uninstalled package, auto-apt will install that package using apt-get. This feature requires apt and sudo to work.

But again this method fails your "no typing" requirement.

Other ideas

I believe the facility that is providing the recommendation to install gparted is tied into the shell using a tool called completion. There are rules that you can expand upon so that various activities happen when these rules are matched.

One could probably expand on these rules, or possibly 1 rule, so that the apt-get -y install <package X> runs automatically.

  • put it in a startup script. then you don't need to type, unless I'm misunderstanding (disclaimer: I haven't actually looked at anything but this answer) – strugee Nov 6 '13 at 22:39
  • @strugee - he wants it to auto install after completion determines it's not installed. – slm Nov 6 '13 at 22:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.