Reusing output from last command in Bash

后端 未结 13 2081
长情又很酷
长情又很酷 2020-12-07 08:35

Is the output of a Bash command stored in any register? E.g. something similar to $? capturing the output instead of the exit status.

I could assign the

13条回答
  •  一个人的身影
    2020-12-07 08:57

    Like konsolebox said, you'd have to hack into bash itself. Here is a quite good example on how one might achieve this. The stderred repository (actually meant for coloring stdout) gives instructions on how to build it.

    I gave it a try: Defining some new file descriptor inside .bashrc like

    exec 41>/tmp/my_console_log
    

    (number is arbitrary) and modify stderred.c accordingly so that content also gets written to fd 41. It kind of worked, but contains loads of NUL bytes, weird formattings and is basically binary data, not readable. Maybe someone with good understandings of C could try that out.

    If so, everything needed to get the last printed line is tail -n 1 [logfile].

提交回复
热议问题