1

Shells have a useful feature where pressing the keyboard UP arrow replaces the contents of the current command line with the previous command that was entered. (And if you do it again, the command before that, and so on.)

I would like a similar feature, where pressing (some other button) replaces the contents of the current command line with the last line of output from the previous command (and if you do it again, the previous line of output , and so on.)

Is there any shell that provides a feature like this?

The motivation is that you often want to run a command, and then run another command with part of the previous commands's output as input. (No, I don't want to do anything involving piping the output of one command into the next. I'm looking for an interactive experience, where you run the first command, see its output, and then get a line of that output into your next command, without having to reach for the mouse to do a clumsy copy-and-paste.)

3

If you run your shell inside screen, you can use screen’s scrollback mode to achieve this. Using the default key bindings:

  • CtrlAEsc enters scrollback mode;
  • movement keys move up and down;
  • Y copies the current line to the paste buffer and leaves scrollback mode;
  • CtrlA] pastes the buffer.

The documentation (run man screen) gives lots more detail and describes the other features of scrollback mode (including searches, partial copies etc.).

tmux no doubt has similar features.

0

If I read that correctly, you want to take last line from the output of one command, and execute that as a new command. Thus:

$ $( /path/to/command_generator | tail -n1 )

Or, if it's the contents of a file on disk:

$ $( tail -n1 /path/to/RUNME.txt )

If you want to edit this before execution, you can use vipe, which is a tool to edit the contents of pipelines in-flight:

$ $( /path/to/command_generator | vipe )
  • I did say I don't want to pipe the output of the first command into the next :) – HighCommander4 Oct 6 '17 at 16:45
  • vipe if not given an output to pipe into, will dump into standard output, which is then executed since that output is in a $( ) "execute-me" subshell. – DopeGhoti Oct 6 '17 at 16:50
  • Ok, I kind of see how that approximates what I'd like. Still not quite there, though, as I still need to anticipate the fact that I'll need to run a follow-up command before running the first command. Sometimes, it's only after seeing the output of the first command, that I realize I want to run a second. So I'm looking for some "after the fact" access to the previous command's output. The "screen" feature from the other answer seems to fit the bill. – HighCommander4 Oct 6 '17 at 16:53
0

Maybe you want `!!` to re-execute the previous command and use it's output inline, say looking for a file:

$ find . -name "myfile.txt"
./dir/files/myfile.txt
$ emacs `!!` &
emacs `find . -name "myfile.txt"` &

which would equate to:

$ emacs ./dir/files/myfile.txt &
  • Of course that is useful per se only if the previous command had only one line of output  (and you want to use it without modification). – Scott May 21 at 17:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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