本文介绍了导出在我的 shell 脚本中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个脚本 1.sh 和 2.sh.

1.sh如下:

#!/bin/sh变量=thisisit"导出变量

2.sh如下:

#!/bin/sh回声$变量

根据我读到的内容,这样做(导出)可以从另一个 shell 脚本访问一个 shell 脚本中的变量.但这在我的脚本中不起作用.有人可以帮忙吗.提前致谢.

解决方案

如果你正在执行像 sh 1.sh./1.sh 这样的文件,那么你在子shell中执行它.

如果您希望在当前 shell 中进行更改,您可以执行以下操作:

.1.sh# 或者源 1.sh

请考虑阅读参考文档.

当使用 source [或 .] 运行脚本时,它在现有 shell 中运行,脚本创建或修改的任何变量将保持可用在脚本完成后.相反,如果脚本以 filename 的形式运行,那么将生成一个单独的子 shell(具有一组完全单独的变量)来运行脚本."

I have two scripts 1.sh and 2.sh.

1.sh is as follows:

#!/bin/sh
variable="thisisit"
export variable

2.sh is as follows:

#!/bin/sh
echo $variable

According to what I read, doing like this (export) can access the variables in one shell script from another. But this is not working in my scripts. Can somebody please help. Thanks in advance.

解决方案

If you are executing your files like sh 1.sh or ./1.sh Then you are executing it in a sub-shell.

If you want the changes to be made in your current shell, you could do:

. 1.sh
# OR
source 1.sh

Please consider going through the reference-documentation.

"When a script is run using source [or .] it runs within the existing shell, any variables created or modified by the script will remain available after the script completes. In contrast if the script is run just as filename, then a separate subshell (with a completely separate set of variables) would be spawned to run the script."

这篇关于导出在我的 shell 脚本中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-30 08:20