在shell脚本中输出命令

当我在命令提示符下使用导出命令时,它按预期工作。 但是它不能从shell脚本中运行。

[root@server shantanu]# export myip=10 [root@server shantanu]# echo $myip 10 [root@server shantanu]# vi myip.sh #!/bin/sh export myipadd=10 [root@server shantanu]# sh -xv myip.sh #!/bin/sh export myipadd=10 + export myipadd=10 + myipadd=10 [root@server shantanu]# echo $myipadd 

我想在下次运行时将variables提供给同一个脚本。 换句话说,我正在寻找一些方法来记住variables的值。

从命令行执行export命令使环境variables仅在当前会话中生效。 将它添加到你的shell启动文件中以便永久使用。

例如,用bash:

 echo "export myipadd=10" >> ~/.bash_profile (for only root) echo "export myipadd=10" >> /etc/profile (for all users) 

如果您需要在shell脚本中导出variables并使其可用于其他脚本,则需要使用source命令。

 $ cat test.sh export MY_VAR="hello" $ source test.sh $ echo $MY_VAR hello