Reusing output from last command in Bash

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

    If you are on mac, and don't mind storing your output in the clipboard instead of writing to a variable, you can use pbcopy and pbpaste as a workaround.

    For example, instead of doing this to find a file and diff its contents with another file:

    $ find app -name 'one.php' 
    /var/bar/app/one.php
    
    $ diff /var/bar/app/one.php /var/bar/two.php
    

    You could do this:

    $ find app -name 'one.php' | pbcopy
    $ diff $(pbpaste) /var/bar/two.php
    

    The string /var/bar/app/one.php is in the clipboard when you run the first command.

    By the way, pb in pbcopy and pbpaste stand for pasteboard, a synonym for clipboard.

提交回复
热议问题