How to avoid heredoc expanding variables? [duplicate] How to avoid heredoc expanding variables? [duplicate] bash bash

How to avoid heredoc expanding variables? [duplicate]


Just use 'EOF' to prevent the variable from expanding:

sudo /bin/su -c "cat << 'EOF' > /etc/init.d/my-script#                       ^   ^

From man bash:

Here Documents

This type of redirection instructs the shell to read input from thecurrent source until a line containing only delimiter (with notrailing blanks) is seen. All of the lines read up to that point arethen used as the standard input for a command.

The format of here-documents is:

      <<[-]word              here-document      delimiter

No parameter expansion, command substitution, arithmetic expansion,or pathname expansion is performed on word. If any characters in wordare quoted, the delimiter is the result of quote removal on word, andthe lines in the here-document are not expanded. If word isunquoted, all lines of the here-document are subjected to parameterexpansion, command substitution, and arithmetic expansion. In thelatter case, the character sequence \<newline> is ignored, and \must be used to quote the characters \, $, and `.


when using the su command put the command itself in sigle quotes and just escape the $ with a backslash. the placeholder variables has to set in command bash context (here after su). so you need to do sth like

su -c 'ph="ph"; cat << EOF > script varinscript=$phvar=\${var}EOF'