Search code examples
shellunixcopy-pastezsh

Is is possible to put into clipboard the result of a shell command?


Imagine I do:

echo $PATH

in a terminal. Is is possible that the result is automatically copied so that if I do Ctrl+y it would get printed? As I understand it, when doing Ctrl+k on a terminal, the text is saved in a memory buffer that belongs to the terminal, so I would think something like this should be possible.

Any thoughts?


Solution

  • Depends. Linux, Mac or Windows?

    The mac has the commands pbcopy and pbpaste to copy and paste something from the clipboard.

    Copy example (mac):

    echo $PATH | pbcopy
    

    Paste Example (mac):

    echo "$(pbpaste -Prefer txt)"
    

    Linux uses X which has multiple copy-paste buffers (somewhat akin to the clipboard, but a little more involved).

    You can use a little application like XSel to copy/paste, The command would be used in the same form as the pbcopy/pbpaste

    Copy:

    echo $PATH | xsel --clipboard
    

    'paste':

    echo "$(xsel --output --clipboard)"
    

    For windows, you can use an app like clip, which allows the same copy/paste functionality

    Copy:

    set %PATH% | clip
    

    I generally use Linux/Unix so I don't have the equivalent for pasting from the clipboard on Windows.