$ 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
为什么我在输出中得到-e ? crontab和bash都使用/bin/echo
他们可能不是都使用/bin/echo 。 cron使用的环境可能与您的交互式环境有所不同。 指定完整的path来确保。
这是人们推荐使用printf而不是echo来实现可移植性的原因之一。
* * * * * printf "test1\ntest2\n" >> logfile
cron使用的shell是sh而不是Bash。 在我的系统上, sh是真正的Dash,它的echo没有-e 。 因此-e变成另一个要输出的string。 其他版本的Bourne shell或提供其function的shell可能具有-e 。
如果你在你的crontab没有完整path的情况下使用echo ,你会得到shell的内buildecho而不是/bin/echo 。 在命令行上,如果你使用的是Bash,那么没有完整path的echo会给你Bash的内置版本。