Casper Casper - 1 year ago 92
Bash Question

How to echo exactly this string containing of special characters

I need to write exactly this string into CRON programmatically, but when I do:

echo "*/2 * * * * for i in `docker ps -a| grep -v Up | awk '{print $NF}'`; do ~/./ $i; done >/dev/null 2>&1" >> mycron

I got:

neo4j; do ~/./ neo4j; done >/dev/null 2>&1

So how can I escape all those special characters and prevent the script being executed.

Answer Source

use single quotes to prevent bash/shell expansion/evaluation. You also need to escape the single quotes around the awk script as below.

$ echo '*/2 * * * * for i in `docker ps -a| grep -v Up | awk '\''{print $NF}'\''`; do ~/./ $i; done >/dev/null 2>&1' >> mycron
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download