Reusing output from last command in Bash

后端 未结 13 2052
长情又很酷
长情又很酷 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 09:04

    Very Simple Solution

    One that I've used for years.

    Script (add to your .bashrc or .bash_profile)

    # capture the output of a command so it can be retrieved with ret
    cap () { tee /tmp/capture.out}
    
    # return the output of the most recent command that was captured by cap
    ret () { cat /tmp/capture.out }
    

    Usage

    $ find . -name 'filename' | cap
    /path/to/filename
    
    $ ret
    /path/to/filename
    

    I tend to add | cap to the end of all of my commands. This way when I find I want to do text processing on the output of a slow running command I can always retrieve it with res.

    0 讨论(0)
提交回复
热议问题