Strange problem with crontab and echo
Solution 1
They're probably not both using /bin/echo
. The environment used by cron
is probably different from your interactive environment. Specify the full path to be sure.
Solution 2
This is one of the reasons people recommend using printf
instead of echo
for portability.
* * * * * printf "test1\ntest2\n" >> logfile
The shell that cron
uses is sh
rather than Bash. On my system, sh
is really Dash and its echo
doesn't have -e
. So -e
becomes just another string to be output. Other versions of the Bourne shell or shells that provide its functionality may have -e
.
If you use echo
without the full path in your crontab
, you're getting the shell's builtin echo
instead of /bin/echo
. On the command line, if you're using Bash, then echo
without the full path gives you Bash's builtin version.
Related videos on Youtube
aidan
Updated on September 17, 2022Comments
-
aidan over 1 year
$ echo -e "test1\ntest2" > logfile $ echo -e "test1\ntest2" >> logfile $ cat logfile test1 test2 test1 test2 $ rm logfile $ crontab -e * * * * * echo -e "test1\ntest2" >> logfile $ sleep 160 $ cat logfile -e test1 test2 -e test1 test2
Why am I getting the
-e
in the output? Both crontab and bash are using/bin/echo
-
aidan almost 14 yearswell, before I tried this test, I tried another test:
* * * * * which echo > logfile
-- it showed the same thing aswhich echo
-
aidan almost 14 years...but specifying the full path as you suggest does fix the problem. Strange?!
-
Janne Pikkarainen almost 14 yearsaidan: "which" doesn't search for shell built-ins, it looks for files under $PATH. That's why "which echo" shows identical results, and that's why the interactive environment and cron can differ.
-
labradort almost 14 yearsGolden rule of crons: Always use full paths. If you always follow the rule, you won't get something unexpected or borken.
-
medina almost 14 years
type echo
should provide you some clarity here :)