使用JavaScript,我们可以执行以下操作:
const foo = function(a,b,c){
console.log(Array.from(arguments));
};
const bar = foo.bind(null,'this is 1');
bar(2,3);
我们将获得以下输出:
[ 'this is 1', 2, 3 ]
有什么办法可以通过bash来做这种事情吗?
我正在尝试存储/导出对参数的引用。但是,我读到bash无法导出数组,例如declare -a arr=()
...有什么方法可以存储参数,以便以后可以再次使用它们,例如重试功能?
也许是这样的:
export some_val=1;
outer(){
local v="$some_val"
inner(){
echo "$v" "$@"
}
export -f inner;
}
那么它将是:
outer
inner '2' '3'
bash技术无法工作的原因不止一个-一个-您只能使用一个内部定义,因为新def将覆盖旧定义...但是更重要的是,它不记得本地值如果在调用外部之后调用它,则为v。
答案 0 :(得分:2)
bash
没有复杂的词法作用域,也没有闭包。您必须使用全局变量来模拟所需的内容。
您可以使用数组来保存多个值。由于所有功能都在同一shell进程中运行,因此您无需导出它。
declare -a v
outer() {
v=("$@")
}
inner() {
echo "${v[@]}" "$@"
}
outer 1
inner 2 3 # prints 1 2 3
outer 15 16 17
inner 5 6 # prints 15 16 17 5 6