1

This question already has an answer here:

I am having trouble with writing case $1 in a bash file. I tried with

cat <<EOT > /etc/init.d/startup.sh
#!/bin/bash

PATH=/sbin:/bin:/usr/sbin:/usr/bin

case "$1" in
    start)
        bash /root/install.sh >> /root/installation_log.txt 2>&1
        ;;
    stop|restart|reload)
        ;;
esac
EOT

But the problem is it writes everything to startup.sh but $1. Line

case "$1" in  

become

case "" in 

after the operation.
What to do?

marked as duplicate by terdon Aug 25 '16 at 12:59

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

1

Quote the EOF terminator passed to the << operator (in any way):

cat << 'EOT' > file
$var
EOT

Or

cat << \EOT
cat << EO\T
cat << "E"'O'T
cat << ""EOT

That's the documented and standard way to prevent any type of expansion inside the here-document.

  • Wow! Awesome. Could you please add some reference? – haccks Aug 25 '16 at 13:00
  • @haccks, see edit with link to the POSIX shell specification. – Stéphane Chazelas Aug 25 '16 at 13:06
1

Change the first EOT into 'EOT' to prevent variable expansion

eg

cat <<'EOT' > /etc/init.d/startup.sh

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