You can remove the unwanted characters with ${var//pattern/replacement}
:
s='-w o rd'
s=${s//[- ]}
And then use the substring expansion to pick one character at a time:
$ for ((i=0; i < ${#s}; i++)); do
echo ${s:i:1}; # or do whatever you like here
done
(it even seems to work with multi-byte characters, at least on my system.)
Note that IFS='\0- '
will assign a literal backslash to IFS
as backslash has no special meaning within single quotes. The octal escape would work within $'...'
, but variables in Bash can't contain a NUL byte, so that doesn't help.
(The string gets cut on the NUL, so e.g. x=$'foo\0bar'; printf "%q\n" "$x"
prints just foo
. Also, what @muru said, splitting on a NUL byte is not the same as splitting between every character)