0

I'm using bash. I'd like to iterate a listing of directory names containing spaces for processing. Here is the directory list

"aaa"
"bbb 111"
"ccc"

Using the following script...

$ for i in $(ls -d */); do echo ${i}; done
aaa
bbb
111
ccc

I want it to show properly as...

aaa
bbb 111
ccc

How can I do it?

bzbdev001
  • 45
  • 5
  • [Do not use ls and what to use instead](https://unix.stackexchange.com/questions/128985/why-not-parse-ls-and-what-to-do-instead) https://stackoverflow.com/questions/7039130/iterate-over-a-list-of-files-with-spaces https://stackoverflow.com/questions/301039/how-can-i-escape-white-space-in-a-bash-loop-list https://stackoverflow.com/questions/53188059/for-loop-through-files-with-spaces-in-their-names https://stackoverflow.com/questions/45301937/loop-through-filenames-with-spaces-within-a-path https://stackoverflow.com/questions/54793461/loop-through-filenames-with-spaces-bash-using-variables – KamilCuk Mar 09 '21 at 10:08
  • 1
    Anyway, just `for i in */; do`... – KamilCuk Mar 09 '21 at 10:11
  • This is a common bash pitfall. The best source I know describing the problem and its workarounds is [this article](https://mywiki.wooledge.org/BashPitfalls#for_f_in_.24.28ls_.2A.mp3.29). – timgeb Mar 09 '21 at 10:49
  • There's still more info & options in [BashFAQ #20: How can I find and safely handle file names containing newlines, spaces or both?](https://mywiki.wooledge.org/BashFAQ/020) – Gordon Davisson Mar 09 '21 at 18:24

0 Answers0