从子进程引用脚本文件中的变量

从子进程引用脚本文件中的变量

a.sh

#!/bin/bash

foo=':bar baz:'
declare varvar=aaaaaa
export xyz=abc

echo "$foo ok"
hoge=huga env piyo=alice bash -i

运行./a.sh|是光标,>是提示符):

:bar baz: ok
> |

有输入:

echo "foo: '$foo'"
echo "varvar: '$varvar'"
echo "xyz: '$xyz'"
echo "hoge: '$hoge'"
echo "piyo: '$piyo'"

预期输出:

> echo "foo: '$foo'"
foo: ':bar baz:'
> echo "varvar: '$varvar'"
varvar: 'aaaaaa'
> echo "xyz: '$xyz'"
xyz: 'abc'
> echo "hoge: '$hoge'"
hoge: 'huga'
> echo "piyo: '$piyo'"
piyo: 'alice'
> |

实际输出:

> echo "foo: '$foo'"
foo: ''
> echo "varvar: '$varvar'"
varvar: ''
> echo "xyz: '$xyz'"
xyz: 'abc'
> echo "hoge: '$hoge'"
hoge: 'huga'
> echo "piyo: '$piyo'"
piyo: 'alice'
> |

如何获取$foo$varvar正确显示?

答案1

使固定a.sh

#!/bin/bash

set -a
foo=':bar baz:'
declare varvar=aaaaaa
export xyz=abc

echo "$foo ok"
hoge=huga env piyo=alice bash -i

它允许导出所有变量。

相关内容