linux - bash - automatically capture output of last executed command into a variable -


i'd able use result of last executed command in subsequent command. example,

$ find . -name foo.txt ./home/user/some/directory/foo.txt 

now let's want able open file in editor, or delete it, or else it, e.g.

mv <some-variable-that-contains-the-result> /some/new/location 

how can it? maybe using bash variable?

update:

to clarify, don't want assign things manually. i'm after built-in bash variables, e.g.

ls /tmp cd $_ 

$_ holds last argument of previous command. want similar, output of last command.

final update:

seth's answer has worked quite well. couple of things bear in mind:

  • don't forget touch /tmp/x when trying solution first time
  • the result stored if last command's exit code successful

this hacky solution, seems work of time. during testing, noted didn't work when getting ^c on command line, though did tweak bit behave bit better.

this hack interactive mode hack only, , pretty confident not recommend anyone. background commands cause less defined behavior normal. other answers better way of programmatically getting @ results.


that being said, here "solution":

prompt_command='last="`cat /tmp/x`"; exec >/dev/tty; exec > >(tee /tmp/x)' 

set bash environmental variable , issues commands desired. $last have output looking for:

startide seth> fortune courtship marriage, witty prologue dull play.                 -- william congreve startide seth> echo "$last" courtship marriage, witty prologue dull play.                 -- william congreve 

Comments

Popular posts from this blog

Cursor error with postgresql, pgpool and php -

c# - how to write client side events functions for the combobox items -

ssh client with paramiko (python) -