For the following bash script
sudo -u root bash << EOF
FILE="defaults.txt"
if [ ! -e "$FILE" ]; then
echo "min_granularity_ns" > $FILE
fi
EOF
I get this error:
bash: line 3: syntax error near unexpected token `newline'
bash: line 3: ` echo "min_granularity_ns" > '
Don't know what is wrong with that. If I use `echo "min_granularity_ns" > defaults.txt, there is no problem. How to fix that?
Escape the $
to prevent the shell from expanding $FILE
:
if [ ! -e "\$FILE" ]; then
echo "min_granularity_ns" > \$FILE
fi
Note: This is not a great answer, but it does explain the problem: variable expansion before the here doc is created. So, I'm leaving it for now.