3

I have been looking around, but I haven't found any answer so far. I Bash/Zsh, I know of a variation of

echo !! 
echo !$

and so on. But is there a way to recall the last output line? I know I could simply edit the line and add some piping information but I was wondering whether there were an alternative. To illustrate, I might want to do the following

ls -tr
vim {whatever key to recall the last line}

which would allow me to edit the most recent file.

If a similar question was already asked and I was unable to find it, could you please redirect me to it?

clem steredenn
  • 352
  • 7
  • 20
  • no, the shell doesn't keep track of the output of any command, it's not it's job. also, this is probably off topic on SO. – mata Feb 21 '14 at 12:50
  • thanks for your answer mata. I have seen some "similar" enough question on SO, so I thought it would be ok. Might be wrong though... – clem steredenn Feb 21 '14 at 12:56
  • If you use `tmux`, you *might* be able to script it to copy the last line of its output buffer. It would not be straightforward, and more work than I can devote to figure out right now. Hopefully, this comment will inspire someone (or my future self) to investigate the details and post an answer. – chepner Feb 21 '14 at 14:15

2 Answers2

4

I think you're out of luck. Potentially the shell would have to store a huge amount of info (imagine an long running process and its output).

For your example:

ls -tr
vim {whatever key to recall the last line}

perhaps an acceptable alternative is to use a subshell thus:

ls -tr
vim $(!!)

which re-executes your last command in a subshell and the output is used as the argument to vim.

Brian Agnew
  • 268,207
  • 37
  • 334
  • 440
  • Thanks for your answer. That could be an option, indeed. – clem steredenn Feb 21 '14 at 12:59
  • re- "Potentially the shell would have to store a huge amount of info" That huge amount of info is already stored anyway — it's the visible scroll-back buffer. But that's not in the domain of the shell, rather the terminal. – K3---rnc Mar 01 '14 at 18:58
  • The visible scrollback buffer is usually limited, isn't it ? – Brian Agnew Mar 02 '14 at 19:31
2

Ok, there is a way to accomplish your goal. This is just a proof-of-concept, I wouldn't really recommend to do so:

> script --flush /tmp/myscript
Script started, file is /tmp/myscript
> LASTLINE() {
     tail -2 /tmp/myscript | head -1 | tr -d '\r'
  }
> bind '"^[[15~":"$(LASTLINE)\n"'
> ls -tr1
file1
file2
fileN
> vi [press F5]

The idea: use script as the tool which remembers in- and output. In the example, it stores all data in file "/tmp/myscript" which in turn can be read to get the last result line. Since the actual command (vi ...) is the last line in "myscript", we read the last two lines of that file and use the first one of them. (Additionally, I had to filter the return character via tr.) Now we bind this command LASTLINE to F5 (look at In bash, how do I bind a function key to a command? to see in detail how to bind a command to a key). And from now on, you can press F5 to get the contents of the last output line.

If you do so, remember that you are within a sub-shell (because of script) which can be left via exit to get back to your original shell.

Careful: this is more or less playing-with-bash. There are race-conditions and other draw-backs, so I'd recommend not to use this solution in sensitive environment.

Community
  • 1
  • 1
leu
  • 2,051
  • 2
  • 12
  • 25