读书人

shell脚本中source有关问题

发布时间: 2012-03-31 13:13:26 作者: rapoo

shell脚本中source问题
shell脚本(test.sh)如下:

#!/bin/sh

echo "export monitor=1" >> /etc/profile
source /etc/profile

调用后(./test.sh),执行echo $monitor,没有输出任何值

手动source /etc/profile后,再次执行echo $monitor,输出了预期的值

可见脚本中的source /etc/profile没有成功,请问这个应该怎么解决?


[解决办法]
test.sh执行的时候是monitor是有值的. 但是test.sh是在一个shell里执行的,test.sh结束后,shell退出了。
回到了楼主登录用shell,由于子进程的环境变量不影响父进程,所以echo 的时候是没有值的。

读书人网 >UNIXLINUX

热点推荐