父shell和子shell 变量问题
父shell 和 子 shell,那么会想到 export 这个命令。export 也是 bash 的一个内置命令。它主要是用来将父 shell 里的变量导出供子 shell 使用。它有如下特征:
1. 用 export 导出的变量放在“导出变量列表”中,它可以被子 shell (子 shell 的子 shell 也是如此)拷贝并使用。
2. 被 export 出来的变量虽然可以被子 shell 使用,但它也只是一个拷贝,而不会影响到父 shell 中的值以及其它子 shell 中的值。
=======================================================================================
在父shell中可以用export 来导出一个变量,在子shell 中就可以继承该变量,但在子shell中用export 导出
的变量并不能影响父shell变量的值
=========================================================================================
子 shell 向父 shell 传递自己的变量方法:
通过一个中间文件进行:
#!/bin/bash
(
subvar="hello shell"
echo "$subvar" > temp.txt
)
read pvar < temp.txt
echo $pvar
-------------------------------------------------------------------------------------------------
[*] 通过命令替换:
#!/bin/bash
pvar=`subvar="hello shell";echo $subvar`
echo $pvar
执行命令替换符(两个反单引号)之间的命令也是在子 shell 来完成的。
----------------------------------------------------------------------------------------------
[*] 使用命名管道:
#!/bin/bash
mkfifo -m 777 npipe
(
subsend="hello world"
echo "$subsend" > npipe &
)
read pread < npipe
echo "$pread"
exit 0
运行输出:
beyes@debian:~/shell$ ./var.sh
hello world
关于有名管道创建命令 mkfifo 可参考:http://www.groad.net/bbs/read.php?tid-3707.html
[*] 使用 here 文档:
#!/bin/bash
read pvar
页:
[1]