2

In Bash, I'd like to create a binding in my .inputrc that makes use of the !! built-in to repeat the last command. But this doesn’t seem to properly expand the !!

bind -x '"\C-t": echo $(!!)'

When I invoke the above binding (Ctrl+t) I just get this:

-bash: !!: command not found

Likewise the simpler

 bind -x '"\C-t": echo !!'

Just yields

!!

Instead of the actual command. Obviously my real use-case is more substantive than this example, this is just an illustration of the problem.

Edit:

This question has nothing to do with echo "#!" fails -- "event not found" which it is claimed mine is a duplicate of. That question pertains to a generalized failure of !* expansion in regular bash due to quoting issues or lack of history. My question on the other hand is very specific to the context of being used inside an .inputrc file (or perhaps an alias), where a different set of factors come into play. On my regular command line, the so-called "bang expansions" have always worked fine. It's only in these special contexts where the problems arose, and hence led to this question.

Community
  • 1
  • 1
Magnus
  • 10,736
  • 5
  • 44
  • 57
  • I think it is possible that under the `bind` command `!!` is not considered as a command. Your last example is so obvious, it is considered as a literal letter. – fronthem Sep 16 '15 at 19:11
  • Maybe you can done it by something like this `eval $(tail -2 ~/.bash_history | head -1)` instead of `echo !!`. I'm not sure but please try. – fronthem Sep 16 '15 at 20:04

2 Answers2

0
"\C-t": "fc -s\n"

fc -s re-executes the last command, and fc is also a builtin:

$ type fc
fc is a shell builtin
Zombo
  • 1
  • 62
  • 391
  • 407
0

Well it seems there's something odd going on with shell quoting.

This does not work

bind -x '"\C-l": "!! \n"'

But putting this in .inputrc does

"\C-l": "!! \n"

Don't really understand why the former doesn't work, but at least the latter does.

EDIT: OK got it. Apparently the "-x" isn't needed when defining on the cmd line. So we can simply write

bind '"\C-l": "!! \n"'

Using -x tells bind not to interfere with what you've already typed (almost like a primal ncurses approximation of a modal window!) which is not what I'm after. Thanks to the accepted answer on In bash, how do I bind a function key to a command? for this insight!

Community
  • 1
  • 1
Magnus
  • 10,736
  • 5
  • 44
  • 57