21

is there a way to copy a string to clipboard from command line?

To be more specific, I want to make a script which copies my email address to clipboard, so that when I need to insert it several times for logging in / register, I just run the script once and then CMD+V it whenever I need.

I heard of pbcopy, but I think this is not my case. Any suggestion? Many thanks!

  • 2
    What makes you think that `pbcopy` won't work for you? –  Sep 25 '16 at 23:08
  • 9
    `echo 'your-email@example.com' | pbcopy` should do the job — it does for me, at any rate. – Jonathan Leffler Sep 25 '16 at 23:18
  • 1
    @shellter: It is about shell programming on Mac OS X. Since it is about programming, it is on topic for SO. – Jonathan Leffler Sep 25 '16 at 23:18
  • @JonathanLeffler : I guess I was reading too fast, when I see clipboard, command line, Ctrl-V, that doesn't sound like a script to me. Unfortunately, I'd still vote to close as "too board" so I won't be retracting my vote-to-close. Good luck to Andrea but please read http://stackoverflow.com/tour , http://stackoverflow.com/help/how-to-ask , http://stackoverflow.com/help/dont-ask , and http://stackoverflow.com/help/mcve before posting more questions. – shellter Sep 25 '16 at 23:24
  • @JonathanLeffler thanks, that's just what i needed. I was wrong with the syntax. Have a good day! – Andrea Ottaviani Sep 25 '16 at 23:26
  • Also I highly recommend something like ClipMenu which not only gives you a clipboard history where you can select and paste older clipboard items but also gives you snippets where you could have for example your email address, so no need for terminal at all in this case – Can Rau Dec 05 '22 at 18:15

2 Answers2

20

You need to pipe the output of your script to pbcopy

For example:

./somescript.sh | pbcopy
Chad Dienhart
  • 5,024
  • 3
  • 23
  • 30
11

echo 'your-email@example.com' | pbcopy (as @Jonathan Leffler stated above)

Or see the answer for the related question on piping to clipboard on different operating systems: Pipe to/from the clipboard in Bash script

Loren
  • 9,783
  • 4
  • 39
  • 49
GM Lucid
  • 1,404
  • 2
  • 12
  • 15