1

Trying to use Linux bash script to write to a file but unable to write the below line because of the extra " and ' symbols in the line. Is there a better way to write a file with linux bash script or a way that will work?

echo "add_header Content-Security-Policy "default-src 'self' https://*.jsdelivr.n$ >> $file

I'm a noob to bash scripting and appreciate the help.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151
T Phillips
  • 49
  • 5

1 Answers1

1

There are several ways to do this. A heredoc is a good choice:

cat >> $file << \EOF
add_header Content-Security-Policy "default-src 'self' https://*.jsdelivr.n$
EOF

This simply takes everything until the EOF verbatim as input to cat, and writes it to the file. (It's not clear to me if you want a double quote before add_header; if so, prepend it.) Another reasonable alternative is to escape double quotes in a double quoted string:

echo "add_header Content-Security-Policy \"default-src 'self' https://*.jsdelivr.n$" >> $file
William Pursell
  • 204,365
  • 48
  • 270
  • 300