Reusing output from last command in Bash

后端 未结 13 2075
长情又很酷
长情又很酷 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:48

    Inspired by anubhava's answer, which I think is not actually acceptable as it runs each command twice.

    save_output() { 
       exec 1>&3 
       { [ -f /tmp/current ] && mv /tmp/current /tmp/last; }
       exec > >(tee /tmp/current)
    }
    
    exec 3>&1
    trap save_output DEBUG
    

    This way the output of last command is in /tmp/last and the command is not called twice.

提交回复
热议问题