在数组中存储可执行行选项

时间:2016-12-15 11:34:11

标签: bash shell

我有一个数组。

ARR=("option1" "option with space" "option3")

如果我有一个以这种方式运行的可执行文件

executable "option1" "option with space" "option3"

我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:3)

您可以像这样运行:

arr=("option1" "option with space" "option3")

executable "${arr[@]}"

答案 1 :(得分:1)

将数组的内容传递给可执行文件,

<executable> "${ARR[@]}"

在这里重复引用数组的内容非常重要,丢失它会使选项中包含空格。

带有示例的插图: -

dudeOnMac:~$ touch file
dudeOnMac:~$ touch "file with spaces"
dudeOnMac:~$ ls
file            file with spaces
dudeOnMac:~$ touch "file_with@symbols"
dudeOnMac:~$ ls
file            file with spaces    file_with@symbols
dudeOnMac:~$ fileList=("file" "file with spaces" "file_with@symbols")
dudeOnMac:~$ ls "${fileList[@]}"
file            file with spaces    file_with@symbols
dudeOnMac:~$ ls -1tr "${fileList[@]}"
file
file with spaces
file_with@symbols