You can enable it by adding this to your
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'
Apply this patch for Ubuntu from here.
CommandNotFound.py by adding four spaces at beginning of lines 215,216,217 (note: not tabs):
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)
For some options look at:
an/or check out the script itself.