5

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?

Gilles 'SO- stop being evil'
  • 807,993
  • 194
  • 1,674
  • 2,175
ledawg
  • 151
  • 2
  • 3
    see [this recent question](http://unix.stackexchange.com/q/213799/119298) which is similar. – meuh Jul 05 '15 at 21:03
  • possible duplicate of [Construct a command by putting a string into a tty](http://unix.stackexchange.com/q/48103/80216); see also [How to redirect to stdin of a running bash shell?](http://superuser.com/q/403148/354511) – G-Man Says 'Reinstate Monica' Jul 07 '15 at 01:12

3 Answers3

5

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

Evgeny Vereshchagin
  • 5,286
  • 4
  • 35
  • 43
4

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)
  else
    #otherwise, print the contents of the clipboard
    xclip -selection c -o 
  fi
}
cb_edit() { cb | vipe "$@" | cb; }
Petr Skocik
  • 28,176
  • 14
  • 81
  • 141
2

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 !$
Jakuje
  • 20,974
  • 7
  • 51
  • 70