0

我想知道在将值数组传递给像 zip 这样的程序时,bash 中是否有更好的方法,因为 shell 检查会抱怨如示例中所示。第一次调用显示了一种可行的方法,但 shell check 抱怨,第二个 zip 不起作用但 shell check 很高兴。

在关闭这个问题之后,因为我猜想过度简化了这个例子,并使用提供的 lings 尝试了一些事情并提出了这个解决方案。我在 find 调用周围添加了括号并添加了 declare -a。现在两个 zip 调用都有效。

这是解决这个问题的正确方法吗?

在 bash 中解决这个问题的首选方法是什么?

#!/bin/bash

touch file1.txt
touch file2.txt

declare -a filenames=($(find . -name "file*.txt"))
zip_filename="myzip.zip"

#works but shell check complians https://github.com/koalaman/shellcheck/wiki/SC2068
zip -v -j "works_${zip_filename}" ${filenames[@]}

#does not work
zip -v -j "not_working_${zip_filename}" "${filenames[@]}"
4

0 回答 0