Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Using > and variable file name

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Escape the $ to prevent the shell from expanding $FILE:

if [ ! -e "\$FILE" ]; then
    echo "min_granularity_ns" > \$FILE
fi
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading