0

I am trying to execute below script in remote machine but the output is not adding '\n' while executing sed command. But if I execute same sed command outside its working fine. I tried adding \$ in the sed command thats not adding \n to the output. Any thoughts ?


set -e
set -o pipefail

ssh "$host" << EOF

  temp_working_dir=/tmp/working_dir
  mycert_path=\${temp_working_dir}/mycert/

  cert=\$(cat "\${mycert_path}"/cert.pem | sed 's/$/\\n/' | tr -d '\n')

  echo "\$cert" > /tmp/cert
 
EOF
Madhu CM
  • 2,296
  • 6
  • 29
  • 38
  • 2
    Do these answer your question? [How to avoid heredoc expanding variables?](https://stackoverflow.com/questions/27920806/how-to-avoid-heredoc-expanding-variables) or [How to cat <> a file containing code?](https://stackoverflow.com/a/22698106) – markp-fuso Sep 18 '21 at 14:58

1 Answers1

1

escaping escapes is tiring.

your sed script needs yet one more escaping backslash: 's/$/\\n/g'

set -e
set -o pipefail

ssh "${host}" << EOF

    temp_working_dir=/tmp/working_dir
    mycert_path=\${temp_working_dir}/mycert

    cert=\$(sed 's/$/\\\n/g' "\${mycert_path}/cert.pem" | tr -d '\n')

    echo "\$cert" > /tmp/cert
EOF