Command-Line SSH – Copy SSH Public Key to Clipboard Without Newline

command linessh

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-

Best Answer

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