davoid davoid - 1 year ago 96
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 ?

Answer Source

You can split the string with sed(1):

seq=`printf "%s\n" "$seq" | sed 's/./ &/g'`
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download