This question already has an answer here:

I am on the bash shell and I want the output of a command to appear directly in the command prompt that appears after the command has executed !

Example of what I envision it, to illustrate my idea:

locate create_tables.sql|MAGIC_command
user@localhost:~# /usr/share/doc/phpmyadmin/create_tables.sql

Now, using comman dsubstitution like this

sudo $(locate create-tables.sql)

works but immediately executes the output, I'd like to be able to edit it before. Is there a way?

marked as duplicate by G-Man, cuonglm, Anthon, Archemar, lcd047 Jul 7 '15 at 5:08

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.


In Emacs-mode type sudo $(locate create-tables.sql), Esc,Control+e

See shell-expand-line in Bash Reference Manual:

Expand the line as the shell does. This performs alias and history expansion as well as all of the shell word expansions

  • 1
    Or ctrl+alt+e if you have your terminal set up nicely. – Peter Cordes Jul 6 '15 at 0:52

I generally use the clipboard for this kind of thing

$ some_command | cb
$ cb_edit
$ `cb` #or paste it with your paste button/shortcut

The magic:

 cb() {
  if [ ! -t 0 ]; then
    #if somebody's piping in reproduce input and pipe a copy to the clipboard
    tee >(xclip -selection c)
    #otherwise, print the contents of the clipboard
    xclip -selection c -o 
cb_edit() { cb | vipe "$@" | cb; }

Not very elegant solution is to store the output into some tmpfile, edit that file and then run:

$ locate create_tables.sql > /tmp/tmpfile
$ vi !$    # last argument of last command
$ bash !$

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