Skip to main content

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

6
  • 3
    Cleanest, most elegant solution in this bunch, though should re-iterate that each element defined in the array should be quoted.
    – maverick
    Commented Oct 24, 2012 at 4:01
  • While Dan Fego's answer is effective, this is the more idiomatic way to handle spaces in the elements. Commented Jul 11, 2016 at 4:38
  • 3
    Coming from other programming languages, the terminology from that excerpt is really hard to understand. Plus the syntax is baffling. I'd be extremely grateful if you could go into it a bit more? Particularly expands to a single word with the value of each array member separated by the first character of the IFS special variable
    – Jodes
    Commented Oct 6, 2016 at 9:52
  • 2
    Yes, agree the double quotes are solving it and this is better than other solutions. To further explain - most others are just lacking the double quotes. You got the correct: for elem in "${files[@]}", while they have for elem in ${files[@]} - so the spaces confuse the expansion and for tries running on the individual words.
    – arntg
    Commented May 26, 2017 at 17:55
  • This does not work for me in macOS 10.14.4, which uses "GNU bash, version 3.2.57(1)-release (x86_64-apple-darwin18)". Maybe a bug in the older version of bash?
    – Mark
    Commented Apr 17, 2019 at 1:47