I would like to pass whatever the user has typed in a bash shell to a command, when the user presses a keyboard binding|shortcut

For example : Lets say Ctrl+b is our binding

bind '"\C-b": "myCommand.sh"' 

User types tar czv then presses the shortcut—which passes the string 'tar czv' to mycommand.sh

Is this possible at all ? The only way I can think of is to pass it after echoing it.

bind '"\C-b": " \e[1~ echo '\''\e[4~'\''| mycommand.sh\n"'

Is there a better way?

  • Bash and zsh are completely different here, that should be two separate questions. I've removed the mention of zsh from your question since you have a bash answer. – Gilles Jul 15 '15 at 23:07

Not sure I exactly understand, but this seems to do what you want (?).

bind '"\C-b": "\C-a\C-kecho \'\'\\C-y\'\\' | myCommand.sh\n"'

The quoting gets tricky. Easier in ~/.inputrc:

"\C-b": "\C-a\C-kecho \'\C-y\' | myCommand.sh\n"

Instead of echoing, what about having the script take a command line argument instead? Again, in ~/.inputrc:

"\C-b": "\'\C-amyCommand.sh \'\n"

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.