[英]Elements of a bash array as separate command line arguments to python
I have a bash array which have paths in the form of string as its elements. 我有一个bash数组,该数组具有以字符串形式的路径作为其元素。
${arr[0]} = "path/to/json1"
${arr[1]} = "path/to/json2"
${arr[2]} = "path/to/json3"
and so on... 等等...
Now I want to pass all these elements as command line arguments to a python command within the same bash script at the same time 现在,我想将所有这些元素作为命令行参数同时传递给同一bash脚本中的python命令
python3 script.py ${arr[0]} ${arr[1]} ${arr[2]}
But the problem is the number of elements in arr varies and is not fixed. 但是问题是,arr中的元素数量会发生变化,并且不是固定的。 So I can't hard code each element as a separate argument like above.
所以我不能像上面那样将每个元素硬编码为单独的参数。
How can we pass all the elements of a bash array as command line arguments to python command? 我们如何将bash数组的所有元素作为命令行参数传递给python命令?
Use ${arr[@]}
to refer to the entire array. 使用
${arr[@]}
来引用整个数组。
python3 script.py "${arr[@]}"
This isn't specific to Python, it's how you pass all the array elements to any command. 这并非特定于Python,而是将所有数组元素传递给任何命令的方式。
You should also always quote shell variables unless you have a good reason not to. 除非有充分的理由,否则还应始终引用Shell变量。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.