If I assign a variable in Bash like so:

PRIVATE_KEY="$(< ~/.ssh/id_rsa)"

and then immediately dump that to a file:

echo $PRIVATE_KEY > tmp.txt

and then compare this to the original:

diff ~/.ssh/id_rsa tmp.txt

I do not get a match as all of the carriage returns (or maybe it's line feeds) have been stripped off in the original variable assignment. How do I avoid this?


Use quotes when you echo the variable to the file:

echo "$PRIVATE_KEY" > tmp.txt

That will preserve line breaks and spacing.

  • So simple but so perfectly right. Thanks. :) – ken Oct 7 '14 at 12:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.