我试图让bash函数调用另一个使用bash -c的函数。如何使脚本中先前创建的函数在不同bash会话之间保持不变?
当前脚本:
#!/bin/bash
inner_function () {
echo "$1"
}
outer_function () {
bash -c "echo one; inner_function 'two'"
}
outer_function当前产出:
$ /tmp/test.sh
one
bash: inner_function: command not found期望产出:
one
two发布于 2019-10-07 07:08:13
出口:
typeset -xf inner_function示例:
#! /bin/bash
inner_function () { echo "$1"; }
outer_function () { bash -c "echo one; inner_function 'two'"; }
typeset -xf inner_function
outer_function编写完全相同的东西的其他方法是export -f inner_function或declare -fx inner_function。
请注意,导出的shell函数是a),这是一个仅使用bash的特性,在其他shell和b中不受支持)仍然存在争议,即使大多数bug都是自谢尔休克以来修复的。
发布于 2019-10-07 07:08:58
当我需要在几个脚本中使用相同的函数时,我把它放在一个侧面的“库”脚本文件中,并把它放在需要函数的脚本中(source the_lib_script或. the_lib_script)。
https://unix.stackexchange.com/questions/545464
复制相似问题