2

Is it possible to expand and edit the result of a command substitution $(<cmd>) in Bash?

Suppose that <cmd> results in another command that I want to execute. Indeed, $(<cmd>) will execute that command, but it won't be visible in history, so I can't go back and edit it.

The only way I've solved this problem is to assign the output of $(<cmd>) to a variable, cmd=$(<cmd>), and then echoing that into the clipboard using pbcopy on macOS: echo $cmd | pbcopy.

2

2 Answers 2

4

You can expand all shell substitions in the current command line.

shell-expand-line (M-C-e) - Expand the line as the shell does. This performs alias and history expansion as well as all of the shell word expansions (see Shell Expansions).

Press: Ctrl+Esc+e.

Example:

$ echo $(curl -s ifconfig.io)
# press ctrl+esc+e,
# command line will switch to:
$ echo 151.101.1.69

Note, that I added -s to curl to avoid stderr mingling with the command line. For other commands, you might want to use 2> /dev/null.

3

I use Bash in Kubuntu on a non-Apple laptop. Ctrl+Alt+e expands many things in the command line, including $(whatever). I'm not sure how the keystroke "translates" to the Mac keyboard. The point is to trigger the shell-expand-line command.

Notes:

  • errors from whatever can interrupt your flow
  • other things will be expanded inline as well
  • quoting will be lost

Therefore I think it's good to type $(whatever) first, expand it, only then add the rest.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .