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?
Or, using your EOF markers, you need to quote the initial marker so expansion won't be done:
IHTH
You only need a minimal change; single-quote the here-document delimiter after
<<
.or equivalently backslash-escape it:
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.
will produce
As suggested by @fedorqui, here is the relevant section from
man bash
:This should work, I just tested it out and it worked as expected: no expansion, substitution, or what-have-you took place.
Using the following also works.
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: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:instead of the expected: