8

Is there a way to quickly (e.g. via a keyboard shortcut, etc.) to reference the output of the previous command's output that it wrote to stdout?

For example, if I did this:

which rails

and it returned /usr/local/bin/rails and then I wanted to open that file in textmate, I could re-type the output like this:

mate /usr/local/bin/rails

but is there a way to quickly reference the output without having to re-type it?

NOTE: I am aware I can just do mate $(which rails), but I am specifically looking to reference stdout.

user1516425
  • 1,531
  • 2
  • 15
  • 21
  • 2
    No, it's gone as far as Bash is concerned. It may be available in your terminal's scroll-back buffer (`screen` for example). – Dennis Williamson Jul 11 '12 at 02:41
  • !! only has the input, not the output, the output is not stored anywhere except in the terminal buffer. – pizza Jul 11 '12 at 03:58
  • 1
    Check out this thread: http://stackoverflow.com/questions/5955577 – armandino Jul 11 '12 at 12:41
  • 1
    possible duplicate of [bash - automatically capture output of last executed command into a variable](http://stackoverflow.com/questions/5955577/bash-automatically-capture-output-of-last-executed-command-into-a-variable) – kojiro Jul 12 '12 at 02:58

2 Answers2

4

I use backticks with history reference:

$ which rails
/usr/local/bin/rails
$ mate `!!`

Actually, my editor (a script starting gvim) is aliased to e, so it looks even shorter:

$ e `!!`

and you can always bind to a hotkey (see bash man page for bind command and readline support).

Also, if you can use cut buffers (select with a mouse in an X application), a hotkey for something like the below might be useful:

$ e $(xclip -out)

The command will start the editor as above with whatever was in the cut buffer on command line. Given that many paths are selectable with just a double click, a selected path can be edited very quickly.

fork0
  • 3,401
  • 23
  • 23
  • the \`!!\` way doesn't reference stdout; instead, it reruns the command. you cant test this by making "echo $RANDOM" and then "echo \`!!\`": the values will be different. – asbovelw Jan 09 '20 at 12:22
  • Which is enough for most practical purposes. Wasn't suggesting that all commands can be repeated keeping original effects, but many commands can be. And there is still truck to paste the selection... – fork0 Jan 11 '20 at 09:44
-1

You could always run the command in backticks:

mate `which rails`

I have to say though that it feels a little, uh, risky. What if your PATH has been tampered with so that which returns a different version of rails than what you really needed? What if which returns nothing? So, take care to close down all those error cases, or avoid them in some way (say, reading the path to rails from a config file, and writing a tool that builds that config file for you).

AlwaysLearning
  • 796
  • 3
  • 10