54

How do I copy results from the commandline directly to the clipboard?

On Windows's cmd.exe I can do simply echo "asd" | clip and it pipes output to the clipboard.

I tried to install xclip for that, and though it compiled, when called it prints:

Error: Can't open display: (null)

Using mouse is not the solution.

3
  • Is using the mouse an option? If so, just highlight the text. Commented Mar 31, 2017 at 15:12
  • Definitely not, I googled and saw many suggestions like that :) Commented Apr 1, 2017 at 17:55
  • For users familiar with xclip that combines copy and paste then you might be interested in the paste command.
    – Att Righ
    Commented Aug 23, 2021 at 11:55

4 Answers 4

96

In Build 14393 or later, if you want to copy something to clipboard in WSL console, just add '.exe' to what you do in Windows cmd.

echo "aaa"|clip.exe

To read from clipboard:

powershell.exe -command "Get-Clipboard"
5
  • Cool, works out of the box after upgrading to WSL/Ubuntu 16! Commented Apr 15, 2017 at 22:28
  • 4
    echo didn't work for me, but cat did. That is, entering cat filePath | clip.exe in the command prompt.
    – Jet Blue
    Commented Feb 8, 2018 at 3:21
  • 1
    this works from shell to clipboard, but is there a solution for the reverse? looking for pbpaste or xclip -o equivalent.
    – rev
    Commented Mar 21, 2018 at 1:34
  • 1
    @JetBlue Sounds like you were using echo wrong, then -- echo just prints the text you give it to the output. cat reads a file.
    – anon
    Commented Nov 10, 2018 at 2:14
  • 1
    Something strange happens when I try to use io redirection with the read from clipboard option. I can run it just fine and it will dump the clipboard contents into the command line just fine, but when I try to do e.g. powershell.exe -command "Get-Clipboard" > test.txt, my WSL window appears to lose all personalization settings (font size, typeface) except color scheme. Is this typical?
    – R. Barrett
    Commented Jan 20, 2021 at 23:11
8

In order to copy non-ascii characters (other languages), I had to do this:

echo 'αβψδεφγ' | iconv -f utf-8 -t utf-16le | clip.exe

utf-16le excludes the preceeding BOM so you can paste it back

3
  • I'm unable to run PowerShell scripts on my computer, so I had created a bash script instead. I tried running PowerShell from within the bash script but failed to get the results I wanted, and this solution was the only one that worked.
    – purplecat
    Commented Mar 11, 2020 at 20:17
  • Thanks for sharing! I created an alias for this in my WSL ~/.bashrc file: alias clip='iconv -f utf-8 -t utf-16le | clip.exe' Now I can pipe output to it like echo 'αβψδεφγ' | clip
    – piercebot
    Commented May 21, 2020 at 0:41
  • Perfect, exactly what I needed to get this working on Cygwin. Commented Jan 31, 2021 at 0:54
1

If you would like to have something more 'easy' to use, you can add the commands from @reker to your ~/.bashrc file (if you use zsh, you have to put it in the ~/.zshrc file).

I added these two lines to my file:

alias paste="powershell.exe -command \"Get-Clipboard\""
function clip { "$1" | clip.exe;}

I use clip as a function, so I can use the command linux like ('command' 'use this for command'). If you would prefer the alias way, you can add something like

alias clip=clip.exe

than you don't have to write the .exe all the time.

Don't forget to run the command

source ~/.zshrc

after you saved the file. Otherwise the changes are only applied after a restart of your console.

2
  • I was looking strictly for copying the output of a command as I do that a lot. I never used a paste using command (I always just do shift+insert). But that's a nice suggestion, thanks! Commented Mar 14, 2020 at 0:55
  • There are a few bugs.So you can't copy&paste a path when there is a whitespace in it (using pwd). It doesn't escape them.
    – Romanicus
    Commented Mar 16, 2020 at 9:28
0

This still does not seem to be supported: Can Bash on Windows interact with the system clipboard?.

A clever workaround is the open source tool plak.

Not the answer you're looking for? Browse other questions tagged or ask your own question.