title: How can I access the output history of my shell or terminal-emulator?
body: <p>Terminal emulators obviously save a portion of the output history of any command in a buffer. We know this, because, e.g. in <code>gnome-terminal</code> there are scrollbars on the right and we can scroll back and look at a "history" of what commands we have typed with a limit of scrollback lines.</p>
  
  <p><a href="https://i.stack.imgur.com/azAau.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/azAau.png" alt="scroolback"></a></p>
  
  <p><strong>Is there a way to access this buffered information and use it from within a shell command?</strong> 
  I'm not dead set on <code>gnome-terminal</code> as the emulator. Any of the commonly used, decently featured ones that can do this would be fine.</p>
  
  <p>I now I could just rerun the command, i.e. do something like <code>echo "$(!!)"</code>. But that has two limitations:</p>
  
  <ul>
  <li>The command executes again, with all side-effects, taking time and resources. (which is annoying for long-running commands)</li>
  <li>The command output might change on re-runs (e.g. <code>date</code>)</li>
  </ul>
  
  <p>Here is an example terminal session, that illustrates what I want:</p>
  
  <pre><code>confus@confusion6 ~/devel/dmenu master $ ls
  LICENSE   config.def.h  dmenu.c     dmenu_run  stest    util.c
  Makefile  config.h      dmenu.o     drw.c      stest.c  util.h
  README    config.mk     dmenu.1     drw.h      stest.o  util.o
  arg.h     dmenu         dmenu_path  drw.o      stest.1  safe.c
  
  confus@confusion6 ~/devel/dmenu master $ make
  dmenu build options:
  CFLAGS   = -std=c99 -pedantic -Wall -Os -I/usr/X11R6/include -I/usr/include/freetype2 -D_DEFAULT_SOURCE -D_BSD_SOURCE -D_XOPEN_SOURCE=700 -D_POSIX_C_SOURCE=200809L -DVERSION="4.9" -DXINERAMA
  LDFLAGS  = -L/usr/X11R6/lib -lX11 -lXinerama -lfontconfig -lXft
  CC       = cc
  
  confus@confusion6 ~/devel/dmenu master $ date
  Sa May 11 16:28:08 CEST 2019
  
  confus@confusion6 ~/devel/dmenu master $ get_output_buffer | grep -i sa  # Does not re-run the commands!
  arg.h     dmenu         dmenu_path  drw.o      stest.1  safe.c
  Sa May 11 16:28:08 CEST 2019
  </code></pre>
  
  <p>Here the imaginary function <code>get_outpuf_buffer</code> would access the terminal's output buffer and print that to <code>stdout</code> for further processing. Maybe there is a bash built-in feature that can be used similarly or a terminal-emulator that has my wanted feature?</p>