0

I'm trying to find if the output of the following command, stores just one file in the array array_a

array_a = $(find /path/dir1 -maxdepth 1 -name file_orders?.csv)
echo $array_a
/path/dir1/file_orders1.csv /path/dir1/file_orders2.csv
echo ${#array_a[@]}
1

So it tell's me there's just one element, but obviously there are 2. If I type echo ${array_a[0]} it doesn't return me anything. It's like, the variable array_a isn't an array at all. How can i force it to store the elements in array?

1
  • 1
    Add a shebang and then paste your script there: shellcheck.net Commented Jan 25, 2018 at 5:33

1 Answer 1

3

You are lacking the parentheses which define an array. But the fundamental problem is that running find inside backticks will split on whitespace, so if any matching file could contain a space, it will produce more than one element in the resulting array.

With -maxdepth 1 anyway, just use the shell's globbing facilities instead; you don't need find at all.

 array_a=(/path/dir1/file_orders?.csv)

Also pay attention to quotes when using the array.

 echo "${array_a[@]}"

Without the quotes, the whitespace splitting will happen again.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.