11

I often run commands using subshells, and sometimes would like to have the subshells expanded before I run something.. This way I could verify what I'm doing, and possibly edit what's about to happen as well.

For example, how can I get the following command line to be expanded before I run it, so I can edit the results of the subshell?

e.g.

$ find -name "test.txt" 
/tmp/test.txt

$ mv $(!!) /tmp/new.txt

I'd like to see the subshell expanded before I run the command, like so:

$ mv /tmp/test.txt /tmp/new.txt

Is there some way to do this?

  • 1
    zsh can do this, but I'm fairly certain bash cannot. In zsh if you have your cursor on the parameter, and hit TAB, it'll expand it – Patrick Apr 25 '13 at 13:58
  • I've since switched to zsh and it's great and does this, as you suggest... nice tip! I'll leave the original answer here, as it was a bash question. – Brad Parks Dec 10 '14 at 20:17
5

shell-expand-line (\e\C-e) expands command substitutions in bash.

$ bind -p|grep shell-ex
"\e\C-e": shell-expand-line

$(!!)\e\C-e would run the previous command again and insert the output:

"\eo": "$(!!)\e\C-e"

It also expands other command substitutions, but there is no command like shell-expand-word.

In bash 4.0 or later you could also enable globstar, type **/file.txt, and use glob-complete-word (\eg) or glob-expand-word (\C-x*).

2

Here’s a handy trick — add this line to ~/.inputrc (creating the file if necessary):

Control-x: shell-expand-line

Note you could also either of the following to expand only history, or your aliases, or both:

Control-x: history-expand-line

or

Control-x: alias-expand-line

or

Control-x: history-and-alias-expand-line

This was gleaned from this blog, and this question on SuperUser, which is very similar:

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.