stringbash

Bash: Split string into character array


I have a string in a Bash shell script that I want to split into an array of characters, not based on a delimiter but just one character per array index. How can I do this? Ideally it would not use any external programs. Let me rephrase that. My goal is portability, so things like sed that are likely to be on any POSIX compatible system are fine.


Solution

  • Try

    echo "abcdefg" | fold -w1
    

    Edit: Added a more elegant solution suggested in comments.

    echo "abcdefg" | grep -o .