davoid davoid - 4 months ago 27
Bash Question

Iterating over characters in a string in POSIX sh


read -p "Enter sequence: " seq

for char in $seq; do
echo "$char"

I read sequence of "characters" from standard input. For example let it be

I want to change it to be like this:

'1' '2' '3' '4' '(' ')' ','

I don't want to echo this sequence. I need it to has this new value.

Is it possible in standard sh script ?


You can split the string with sed(1):

seq=`printf "%s\n" "$seq" | sed 's/./ &/g'`