Ubuntu – Not evaluating an expression when using cat

bash

I have one bash script that is designed to output another bash script. I am using cat. I want to evaluate some of the expressions in output script, but not others.

PROJECT=myproject

cat << EOF > create_dir.sh
#!/usr/bin/env bash

DATE=`date '+%y%m%d-%H%M'`
mkdir $PROJECT/\$DATE
EOF

The resulting create_dir.sh file looks like this:

#!/usr/bin/env bash

DATE=171123-1834

mkdir myproject/$DATE

The result that I want is this:

#!/usr/bin/env bash

DATE=`date '+%y%m%d-%H%M'`

mkdir myproject/$DATE

How can I modify this script so that the expression following DATE= is not evaluated, while at the same time ensuring that $PROJECT is evaluated?

Best Answer

You need to escape the '`' symbols since they mean "execute this code". So that line should look like:

DATE=\`date '+%y%m%d-%H%M'\`