Bash – From Multiline Heredocument to a Uniline Herestring with Line Breaks

bashhere-documenthere-stringshell-scriptsyntax

I have this multilinie heredocument which I desire to translate into a uniline herestring:

cat <<-"PHPCONF" > /etc/php/*/zz_overrides.ini
  [PHP]
  post_max_size = 200M
  upload_max_filesize = 200M
  cgi.fix_pathinfo = 0
PHPCONF

The closest I could get is as follows:

cat >"/etc/php/*/zz_overrides.ini" <<< "[PHP] post_max_size = 200M upload_max_filesize = 200M cgi.fix_pathinfo = 0"

but I don't think line breaks after each directive is possible, given the end result is one string. Maybe there is some "unorthodox" way after all?


Both the heredoc, and herestring, are aimed to replace this heavy sed operation:

sed -i "s/post_max_size = .M/post_max_size = 200M/ ; s/upload_max_filesize = .M/upload_max_filesize = 200M/ ; s/;cgi.fix_pathinfo=1/cgi.fix_pathinfo=0/" /etc/php/*/fpm/php.ini

Best Answer

Leaving aside the fact that you could just printf "this\nthat\n" > /some/file, you can make a here-string with newlines with e.g. ANSI-C quoting:

$ cat <<< $'this\nthat'
this
that

Also, because of the quotes, this will try to create a file in a directory literally named *:

cat > "/etc/php/*/zz_overrides.ini"

This would work in Bash, but only if the glob matches one file (if you have /etc/php/foo/zz_overrides.ini and /etc/php/bar/zz_overrides.ini, Bash gives an error)

cat > /etc/php/*/zz_overrides.ini
Related Question