Bash: how to put command output/file contents on the command line

In certain situations, you may want to have a command that you e.g. saved in a file on your command line before executing it. As I didn’t find an answer straight away, here’s a quick-and-dirty way to do it that executes a command when you press a keyboard shortcut:

bind -x '"\C-g":"READLINE_LINE=$(cat dnsmasq_command)"'

This will put the contents of the file named ‘dnsmasq_command’ on your command line when you press Control-G.

And while we’re at it, while it’s got nothing to do with this article, here’s a binding that puts a time stamp (like 20200408022000, no, not a palindrome) on your command line:

bind -x '"\C-g":"READLINE_LINE=${READLINE_LINE:0:$READLINE_POINT}$(date +%Y%m%d%H%M00)${READLINE_LINE:$READLINE_POINT}; READLINE_POINT=$((($READLINE_POINT+14)))"'

One thought on “Bash: how to put command output/file contents on the command line”

  1. A simpler, if not as flexible, way of doing this is to add the following to your ~/.inputrc:

    “\C-o\C-id”: “$(i-date)” # d – date, ISO format: YYYY-MM-DD

    (For me, ‘i-date’ is a shell function that just runs ‘date “+%Y-%m-%d”‘; that can be any command you like, of course.)

    That said, it’s nice to see the actual output of the command on your command line for confirmation, and of course essential if you want to edit it.

Leave a Reply to Curt J. Sampson Cancel reply

Your email address will not be published. Required fields are marked *