208

I want to print code into a file using cat <<EOF >>:

cat <<EOF >> brightup.sh
!/bin/bash
curr=`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
EOF

but when I check the file output, I get this:

!/bin/bash
curr=1634
if [  -lt 4477 ]; then
   curr=406;
   echo   > /sys/class/backlight/intel_backlight/brightness;
fi

I tried putting single quotes but the output also carries the single quotes with it. How can I avoid this issue?

2
  • 8
    You should also fix the shebang. The first line needs to be literally #!/bin/bash and nothing else -- the #! is what makes it into a valid shebang line, and what comes after it is the path to the interpreter.
    – tripleee
    Commented Jan 5, 2015 at 5:15
  • 4
    As a late-coming aside, the modern syntax for process substitution is $(command) instead of `command`. For obtaining the contents of a file, Bash has $(<file)
    – tripleee
    Commented Jun 26, 2015 at 3:26

7 Answers 7

325

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 2018
echo "$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 the current source until a line containing only delimiter (with no trailing blanks) is seen. All of the lines read up to that point are then 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 word are quoted, the delimiter is the result of quote removal on word, and the lines in the here-document are not expanded. If word is unquoted, all lines of the here-document are subjected to parameter expansion, command substitution, and arithmetic expansion. In the latter case, the character sequence \<newline> is ignored, and \ must be used to quote the characters \, $, and `.

For a related problem, see also https://stackoverflow.com/a/54434993

0
39

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

cat <<< '
#!/bin/bash
curr=`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 > file
cd "$HOME"
echo "$PWD" # echo the current path
EOF

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"
3
  • 3
    A here string in single quotes obviously cannot contain any single quotes, which may be a prohibitive issue. Here documents are the only sane workaround if you have a script which needs to contain both single and double quotes, although that is not of course the case with the OP's simple example. Also, tangentially, here-strings <<< are only available starting from Bash 3, and not portable to other shells.
    – tripleee
    Commented Jun 26, 2015 at 3:23
  • <<< is also available in Zsh Commented Oct 13, 2016 at 16:35
  • 3
    today I learned that you can have the filename immediately follow the opening to the heredoc. Thanks @AlexejMagura! Commented Jan 4, 2017 at 6:13
32

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/bash
curr=`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
EOF
4
  • 1
    I would edit your post but just to be safe shouldn't it be #!/bin/bash and not !/bin/bash? Commented Nov 13, 2015 at 23:52
  • @MatthewHoggan : Yep, you'r right! Thanks for catching that. I'm fixing it now.
    – shellter
    Commented Nov 14, 2015 at 1:11
  • excelent. thanks. 'EOF' is the right way
    – acgbox
    Commented Mar 28, 2023 at 16:53
  • ✅ It's very clear & simple.
    – xgqfrms
    Commented Apr 28, 2023 at 6:42
8

I know this is a two year old question, but this is a quick answer for those searching for a 'how to'.

If you don't want to have to put quotes around anything you can simply write a block of text to a file, and escape variables you want to export as text (for instance for use in a script) and not escape one's you want to export as the value of the variable.

#!/bin/bash

FILE_NAME="test.txt"
VAR_EXAMPLE="\"string\""

cat > ${FILE_NAME} << EOF
\${VAR_EXAMPLE}=${VAR_EXAMPLE} in ${FILE_NAME}  
EOF

Will write '"${VAR_EXAMPLE}="string" in test.txt' into test.txt

This can also be used to output blocks of text to the console with the same rules by omitting the file name

#!/bin/bash

VAR_EXAMPLE="\"string\""

cat << EOF
\${VAR_EXAMPLE}=${VAR_EXAMPLE} to console 
EOF

Will output '"${VAR_EXAMPLE}="string" to console'

1
  • The question was actually six years old at the time.
    – tripleee
    Commented Dec 27, 2022 at 8:53
1

cat with <<EOF>> will create or append the content to the existing file, won't overwrite. whereas cat with <<EOF> will create or overwrite the content.

cat test.txt 
hello

cat <<EOF>> test.txt
> hi
> EOF

cat test.txt 
hello
hi

cat <<EOF> test.txt
> haiiiii
> EOF

cat test.txt
haiiiii
2
  • 3
    Doesn't answer the question
    – Unchained
    Commented Oct 20, 2021 at 8:30
  • Your analysis is weird. The heredoc token is <<EOF in both cases, and the unsurprising and well-documented difference between >file and >>file for output redirection is that the former overwrites whereas the latter appends.
    – tripleee
    Commented Dec 27, 2022 at 8:49
1

After conducting several tests, I got a simple but not obvious solution

*task: you need to generate a script inside another script and pass the value of an external variable into internal script

  1. External script "test.sh"

    #!/bin/bash
    
    OUT_FILE=out.sh
    #EXT_MSG=external ## doesnt work
    export EXT_MSG=external
    
    rm -f $OUT_FILE
    
  2. Internal script "out.sh"

    cat <<'EOF'>> $OUT_FILE
    #!/bin/bash
    
    INT_MSG=internal
    
    MSG1=$INT_MSG
    MSG2=$EXT_MSG
    
    echo ${MSG1}
    echo ${MSG2}
    echo "---"
    echo $EXT_MSG
    
    EOF
    
    chmod +x $OUT_FILE
    ./$OUT_FILE
    
  3. Output result

    internal
    external
    ---
    external
    
2
  • I really don't see how these pieces are supposed to fit together or what this adds over previous existing answers. You don't need two separate files, you just need to know how to escape or otherwise protect the dollar signs, backslashes, and backticks which should not be substituted. My answer explains this, though you might want to follow the link to the related answer stackoverflow.com/a/54434993 for more details.
    – tripleee
    Commented Dec 5, 2023 at 12:41
  • @tripleee I had my own task (creating external script inside other) and I solved it exactly as I needed it. My answer shows a more complex example which did not exist in the previous answers. Commented Dec 7, 2023 at 12:25
0

Not sure if doing it without bash counts as an answer.

python -c 'with open("brightup.sh", "w") as fp: fp.write("""
!/bin/bash
curr=`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""")'

or pasting the script into the console (Ctrl+d in the end):

python -c 'import sys; fp= open("brightup.sh", "w"); print("paste script:"); fp.write("".join(sys.stdin.readlines())); fp.close()'
1
  • !/bin/bash is a syntax error, and if you meant to write #!/bin/bash, those need to be the very first bytes in the file for it to be a valid shebang. (You can backslash the newline after the opening """ to get that.)
    – tripleee
    Commented Jun 25 at 11:13

Not the answer you're looking for? Browse other questions tagged or ask your own question.