Reusing output from last command in Bash

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

    One way of doing that is by using trap DEBUG:

    f() { bash -c "$BASH_COMMAND" >& /tmp/out.log; }
    trap 'f' DEBUG
    

    Now most recently executed command's stdout and stderr will be available in /tmp/out.log

    Only downside is that it will execute a command twice: once to redirect output and error to /tmp/out.log and once normally. Probably there is some way to prevent this behavior as well.

提交回复
热议问题