On Mon, 2 Feb 2026, [email protected] wrote:

why bash only, just because

i found this solution that works

Whole lot of text below, but I see no specification of what OP thinks
a word is.

So, Too-Long, Did-Not-Read

BUT, would one/some of you big brained folks elaborate



Using =~ and regular expressions, converting a string to an array in a
single expression:

string="wonkabars"
[[ "$string" =~ ${string//?/(.)} ]]       # splits into array
printf "%s\n" "${BASH_REMATCH[@]:1}"      # loop free: reuse fmtstr
declare -a arr=( "${BASH_REMATCH[@]:1}" ) # copy array for later

The way this works is to perform an expansion of string which substitutes
each single character for (.), then match this generated regular
expression with grouping to capture each individual character into
BASH_REMATCH[]. Index 0 is set to the entire string, since that special
array is read-only you cannot remove it, note the :1 when the array is
expanded to skip over index 0, if needed. Some quick testing for
non-trivial strings (>64 chars) shows this method is substantially faster
than one using bash string and array operations.

The above will work with strings containing newlines, =~ supports POSIX
ERE where . matches anything except NUL by default, i.e. the regex is
compiled without REG_NEWLINE. (The behaviour of POSIX text processing
utilities is allowed to be different by default in this respect, and
usually is.)



Reply via email to