I'm trying to construct an array in bash 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 elementdelimiter.
How can I properly capture the filenames with a space inside the name?
Not exactly an answer to the quoting/escaping problem of the original question but probably something that would actually have been more useful for the op:
Where of course the expression would have to be adopted to the specific requirement (e.g.
*.jpg
for all or2001-09-11*.jpg
for only the pictures of a certain day).There must be something wrong with the way you access the array's items. Here's how it's done:
From the bash manpage:
Of course, you should also use double quotes when accessing a single member
If you had your array like this: #!/bin/bash
You would get:
I don't know why but the loop breaks down the spaces and puts them as an individual item, even you surround it with quotes.
To get around this, instead of calling the elements in the array, you call the indexes, which takes the full string thats wrapped in quotes. It must be wrapped in quotes!
Then you'll get: