I am using bash to get all of my directories with find . -maxdepth 1 -type d
. I am then storing it in an array called selections. The issue is if a file has a space in it then i get some issues. When i try to sort the array, even when changing the delimiter. This is the original set up I tried:
selections=( $(find . -maxdepth 1 -type d) )
unset "selections[0]"; ## removes . as an option
readarray -t sorted_selections < <(IFS=$'\n'; sort <<<"${selections[*]}")
for ((i=0; i<${#sorted_selections[@]}; i++)); do
sorted_selections[i]=${sorted_selections[i]:2}
done
Then when this failed I changing the delimiter to ./
being each result starts with ./, and then removed the code that removed the ./ from the element.
selections=( $(find . -maxdepth 1 -type d) )
unset "selections[0]"; ## removes . as an option
readarray -d ./ -t sorted_selections < <(IFS=$'\n'; sort <<<"${selections[*]}")
for ((i=0; i<${#sorted_selections[@]}; i++)); do
# sorted_selections[i]="${sorted_selections[i]:2}"
done
How can I prevent the sorting from spiting elements for example: taking "New File" to "New" "File"?
selections=( $(find ...) )
did the split at any IFS char, which defaults to including space. If you setIFS=$'\n'
before the first line it works, but is clumsy; see comment on answer