Reputation: 914
I use bash on Ubuntu and I have some files in a folder, some with space in their name, other non.
I would like an array with file's name. Example : [foo.txt, I am a file.txt, bar.jpg, etc.]
My code :
for x in "$(ls -1 test/)"; do
fileList+=($x)
done
I get : [foo.txt, I, am, a, file.txt, bar.jpg, etc.]
If I put fileList+=("$x")
I get one line array [foo.txt I am a file.txt bar.jpg etc.].
How can I do to get what I want?
Thank you.
Upvotes: 0
Views: 105
Reputation: 6984
Why not use shell globs? E.g.
for x in test/*; do
...
or
filelist=( test/* )
EDIT:
shopt -s nullglob
shopt -s dotglob
might be also wanted.
Upvotes: 5
Reputation: 207375
Try using read, like this:
ls | while read f ; do
echo "$f"
done
Upvotes: -1