13

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?

2
  • 2
    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 Commented Apr 25, 2013 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. Commented Dec 10, 2014 at 20:17

2 Answers 2

6

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*).

3

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:

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.