Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

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?

share|improve this question
    
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
add comment

2 Answers 2

up vote 4 down vote accepted

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

share|improve this answer
add comment

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:

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.