I'am trying to write a small script to create an JSON File. I have to store SSL Certificates inside this json, so i have to convert the CRT/KEY file to the required format.

Is there a Solution to store the result of:

cat $domain.key | sed s/$/\\n/ | tr -d '\n'

inside an Variable (bash.script)

input_key=`cat $domain.key | sed s/$/\\n/ | tr -d '\n'`

Does not run the "sed" after the cat (-:


This'll do it:

input_key=$(cat ${domain}.key | sed 's/$/\\n/' | tr -d '\n')

The sed is running in your example, but I believe something in the regex bits are getting interpreted by the shell. You simply need to wrap the regex in single quotes to avoid any shell interpretation. The following also works:

input_key=`cat ${domain}.key | sed 's/$/\\n/' | tr -d '\n'`
  • 2
    UUOC spotted !! – netmonk Sep 22 '15 at 12:45
  • Useless use of tr, as well. – alienth Sep 22 '15 at 12:46
  • That is ! So what is improved version ? – netmonk Sep 22 '15 at 13:03
  • 1
    input_key=$(perl -pe 's!\n!!' ${domain}.key) ;) Why perl? Why not, I say. – alienth Sep 22 '15 at 13:06
  • 1
    and a pure shell version ? – netmonk Sep 22 '15 at 13:08

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.