19

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

2 Answers2

19

You need to pipe the output of your script to pbcopy

For example:

./somescript.sh | pbcopy
Chad Dienhart
  • 4,884
  • 3
  • 22
  • 29
10

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
  • 3,893
  • 2
  • 34
  • 44
GM Lucid
  • 1,355
  • 2
  • 11
  • 14