The use of a *
in "${arr[*]}"
is introducing the first character from IFS as separator for each element of the array. You could try a change of IFS:
readarray -t arr < <(grep -o . <<< "$input") ( IFS=''; echo "${arr[*]}" )
Or try a complex evaluation delay with eval:
readarray -t arr < <(grep -o . <<< "$input") IFS='' eval 'echo "array= ${arr[*]}"'
But there is no real need to start a subshell (to avoid changing the IFS in the present shell) or a risk increasing eval when a simple printf is all you need:
readarray -t arr < <(grep -o . <<< "$input") printf '%s' "${arr[@]}" $'\n'
Note that the use of grep will remove any internal newlines from $input
.
To be able to get the newlines in the array elements (for small inputs):
[[ $input =~ ${input//?/(.)} ]] && arr=("${BASH_REMATCH[@]:1}") printf '%s' array= "${arr[@]}" $'\n'
... grep -o '.\+' <<< "$INPUT"