Set arbitrary text to windows clipboard

1

I'm using clip to set text on the clipboard. To do that, I use echo.

echo Works|clip

The problem is when the text contains special characters, eg. | ".

echo (TRUE|FALSE)|clip
echo " is a Quote Mark|clip

How should the text for echo be escaped? Is there a better way to set text to the clipboard?

Adamarla

Posted 2018-09-12T14:41:01.907

Reputation: 113

Possible duplicate of how to escape pipe symbol | in bat scripts?

– phuclv – 2018-09-12T17:01:33.400

@phuclv Not really. It's a bit more complicated. See my answer. – DavidPostill – 2018-09-12T17:28:16.760

Answers

0

How should the text for echo be escaped?

Use the ^ escape character.

When piping to clip, the ^ needs escaping as well.

Example 1:

> echo (TRUE^^^|FALSE)|clip

Contents of clipboard:

(TRUE|FALSE)

Example 2:

> echo ^" is a Quote Mark|clip

Contents of clipboard:

" is a Quote Mark

Escape Character

^ Escape character.

Adding the escape character before a command symbol allows it to be treated as ordinary text.

When piping or redirecting any of these characters you should prefix with the escape character: & \ < > ^ |

Example:

^\  ^&  ^|  ^>  ^<  ^^

Source Quotes, Escape Characters, Delimiters - Windows CMD - SS64.com


Further Reading

DavidPostill

Posted 2018-09-12T14:41:01.907

Reputation: 118 938

The problem with the " character, is that when present, the text is not piped to the clip command. The |clip is echo'd with the preceding text. The escaped pipe causes an error when piping to clip. – Adamarla – 2018-09-12T16:41:28.290

" needs escaping when piping to clip. Also need to escape ^ when piping to clip. Answer updated. – DavidPostill – 2018-09-12T16:57:22.460