CRON job output using echo with "-e" including "-e"

Code Prowler :

I have a cron job ...

/bin/sh /usr/local/maint/update-wp-sites 2>&1 > /usr/local/maint/output/update-wp-sites.$(date +"\%F_\%H\%M\%S").txt

It works great.

Here's the code for update-wp-sites:

red='\033[0;31m'
color_off='\033[0m'
echo -e "$red updating wp core $color_off"
wp core update

But the output in the update-wp-sites.$(date ...) file specified above looks like this:

-e updating wp core
Success: WordPress is up to date.

Why is "-e" appearing?

rici :

Use printf (which is Posix standard) instead of the non-standard echo -e:

red='\033[0;31m'
color_off='\033[0m'
printf "${red}%s${color_off}\n" "updating wp core"
wp core update

The same applies to echo -n.

Posix does not specify any command-line options for echo, and a Posix-compliant echo simply prints all of its arguments verbatim, including the ones which look like command-line options.

The common (but not universal) extensions to echo can really get in the way: there is no portable way to use them nor to suppress them (in case you wanted to output something starting with -e, for example).

Better to stick with printf, which is a built-in on most shells, which has standardized behaviour, and which is cleanly extensible.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=342034&siteId=1