How to cat <<EOF >> a file containing code? How to cat <<EOF >> a file containing code? unix unix

How to cat <<EOF >> a file containing code?


You only need a minimal change; single-quote the here-document delimiter after <<.

cat <<'EOF' >> brightup.sh

or equivalently backslash-escape it:

cat <<\EOF >>brightup.sh

Without quoting, the here document will undergo variable substitution, backticks will be evaluated, etc, like you discovered.

If you need to expand some, but not all, values, you need to individually escape the ones you want to prevent.

cat <<EOF >>brightup.sh#!/bin/sh# Created on $(date # : <<-- this will be evaluated before cat;)echo "\$HOME will not be evaluated because it is backslash-escaped"EOF

will produce

#!/bin/sh# Created on Fri Feb 16 11:00:18 UTC 2018echo "$HOME will not be evaluated because it is backslash-escaped"

As suggested by @fedorqui, here is the relevant section 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 `.


Or, using your EOF markers, you need to quote the initial marker so expansion won't be done:

#-----v---v------cat <<'EOF' >> brightup.sh#!/bin/bashcurr=`cat /sys/class/backlight/intel_backlight/actual_brightness`if [ $curr -lt 4477 ]; then   curr=$((curr+406));   echo $curr  > /sys/class/backlight/intel_backlight/brightness;fiEOF

IHTH


This should work, I just tested it out and it worked as expected: no expansion, substitution, or what-have-you took place.

cat <<< '#!/bin/bashcurr=`cat /sys/class/backlight/intel_backlight/actual_brightness`if [ $curr -lt 4477 ]; then  curr=$((curr+406));  echo $curr  > /sys/class/backlight/intel_backlight/brightness;fi' > file # use overwrite mode so that you don't keep on appending the same script to that file over and over again, unless that's what you want. 

Using the following also works.

cat <<< ' > file ... code ...'

Also, it's worth noting that when using heredocs, such as << EOF, substitution and variable expansion and the like takes place. So doing something like this:

cat << EOF > filecd "$HOME"echo "$PWD" # echo the current pathEOF

will always result in the expansion of the variables $HOME and $PWD. So if your home directory is /home/foobar and the current path is /home/foobar/bin, file will look like this:

cd "/home/foobar"echo "/home/foobar/bin"

instead of the expected:

cd "$HOME"echo "$PWD"