davoid davoid - 16 days ago 5
Bash Question

Iterating over characters in a string in POSIX sh

#!/bin/sh

read -p "Enter sequence: " seq

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


I read sequence of "characters" from standard input. For example let it be
1234(),


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 ?

Answer

You can split the string with sed(1):

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