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.

Required fields*

3
  • 1
    The second one is nice but it only works (in this case) because you don't quote "$c", so it will drop whitespace and expand stars in $s. Commented Apr 20, 2015 at 3:14
  • 3
    The second option should be: while read -N 1 c; do a+=("$c"); done <<< "$s". The -N allows to read even newlines, and the quotes ("$c") avoid pathname expansion (and some other issues). Commented Aug 16, 2015 at 23:05
  • 1
    Of course, if the last newline must be corrected, change the line given to: while read -N 1 c; do a+=("$c"); done <<< "$s"x and then erase the last newline and the x added: unset a[${#a[@]}-1]; unset a[${#a[@]}-1]. Commented Aug 16, 2015 at 23:20