BASH数组中的元素的空间 [英] BASH array with spaces in elements
问题描述
我试图构建从我的相机文件名的bash的壳数组:
I'm trying to construct an Array in bash-shell of the filenames from my camera:
FILES=(2011-09-04 21.43.02.jpg
2011-09-05 10.23.14.jpg
2011-09-09 12.31.16.jpg
2011-09-11 08.43.12.jpg)
正如你可以看到,在每个文件名中间的空间。结果
我已经试过报价每包的名称,并用反斜杠,无论其作品逃逸的空间。
As you can see, there is a space in the middle of each filename.
I've tried wrapping each name in quotes, and escaping the space with a backslash, neither of which works.
当我尝试访问数组元素,它继续把空间作为元素分隔符。
When I try to access the array elements, it continues to treat the space as the element-delimiter.
我如何正确名称为内部的空间捕捉文件名?
How can I properly capture the filenames with a space inside the name?
推荐答案
我认为这个问题可能与你是如何访问的元素是部分。如果我做一个简单的在$文件
ELEM,我遇到同样的问题,因为你。不过,如果我访问通过其索引数组,像这样,它的工作原理如果我添加数字或与逃逸的元素:
I think the issue might be partly with how you're accessing the elements. If I do a simple for elem in $FILES
, I experience the same issue as you. However, if I access the array through its indices, like so, it works if I add the elements either numerically or with escapes:
for ((i = 0; i < ${#FILES[@]}; i++))
do
echo "${FILES[$i]}"
done
任何 $文件
的这些声明应该工作:
FILES=(2011-09-04\ 21.43.02.jpg
2011-09-05\ 10.23.14.jpg
2011-09-09\ 12.31.16.jpg
2011-09-11\ 08.43.12.jpg)
或
FILES=("2011-09-04 21.43.02.jpg"
"2011-09-05 10.23.14.jpg"
"2011-09-09 12.31.16.jpg"
"2011-09-11 08.43.12.jpg")
或
FILES[0]="2011-09-04 21.43.02.jpg"
FILES[1]="2011-09-05 10.23.14.jpg"
FILES[2]="2011-09-09 12.31.16.jpg"
FILES[3]="2011-09-11 08.43.12.jpg"
这篇关于BASH数组中的元素的空间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!