Bash无法设置环境变量

Jim*_*mmy 0 linux bash shell

我的crontab中有这个:

* * * * * cd /etc && . ./cron.sh>>cron.log
Run Code Online (Sandbox Code Playgroud)

在我的cron.sh(可执行文件)中,我具有:

#!/bin/sh

echo "hello world"
export MyVar="abcd"
Run Code Online (Sandbox Code Playgroud)

它既可以通过cron手动运行,也可以手动运行,但是只有在使用以下命令手动运行时,才设置环境变量:

. ./cron.sh
Run Code Online (Sandbox Code Playgroud)

谁能帮忙。我知道它与源有关,但我无法弄清楚。

这也不起作用:

* * * * * cd /etc && sh ./cron.sh>>cron.log
Run Code Online (Sandbox Code Playgroud)

Mat*_*ieu 5

. will export variables in the current shell, which is the one spawned by the cron, not yours.

If you want to add an extra variable to your shells, use the ~/.profile et al (specifically the /etc/profile that is shared by all users).

  • 或`/ etc / profile`,因为所有用户都已经共享了。 (2认同)