Timeline for Bash array with spaces in elements
Current License: CC BY-SA 3.0
7 events
when toggle format | what | by | license | comment | |
---|---|---|---|---|---|
Apr 16, 2021 at 17:36 | comment | added | user2718593 | IFS=$'\n' IMPORTANT - IFS= Internal Field Separator, changes break point for arrays!!! no break on spaces | |
Apr 17, 2019 at 1:47 | comment | added | Mark | 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? | |
May 26, 2017 at 17:55 | comment | added | arntg |
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.
|
|
Oct 6, 2016 at 9:52 | comment | added | Jodes |
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
|
|
Jul 11, 2016 at 4:38 | comment | added | Daniel Zhang | While Dan Fego's answer is effective, this is the more idiomatic way to handle spaces in the elements. | |
Oct 24, 2012 at 4:01 | comment | added | maverick | Cleanest, most elegant solution in this bunch, though should re-iterate that each element defined in the array should be quoted. | |
Feb 1, 2012 at 0:37 | history | answered | user123444555621 | CC BY-SA 3.0 |