Every Google search for copying a Mac's SSH Public Key to clipboard yields the following command, which results in a newline appended:

pbcopy < ~/.ssh/id_rsa.pub

This is undesirable as I need to paste a Public Key into a variable in an application I'm developing. In the hopes of saving folks the trouble of solving the same problem, I'll post my solution to this question in the answers section-

  • Why not have the application remove the newline before using the public key? – Gordon Davisson Feb 26 '19 at 19:42
  • I'll validate inputs and condition the data later- but for now a command that a user can copy-n-paste to provide the required input is sufficient. Don't have the cycles now to think of every possible way folks can hose their inputs and code logic condition their data. – F1Linux Feb 26 '19 at 20:01

To copy your SSH Public Key cleanly to clipboard WITHOUT a trailing newline use

cat ~/.ssh/id_rsa.pub | tr -d '\n' | pbcopy

or (without cat)

tr -d '\n' < ~/.ssh/id_rsa.pub | pbcopy
  • 3
    Save yourself a command and a subshell tr -d '\n' < ~/.ssh/id_rsa.pub | pbcopy – fd0 Feb 26 '19 at 18:56

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .