Code: Select all
# string='a,b,c,d,e'
# echo -e "${string//,/\n}"
a
b
c
d
e
# printf "${string//,/\n}\n"
a
b
c
d
e
For those that like to work with arrays (non-posix?)
Code: Select all
# IFS=, read -ra array <<<"$string"
# for i in "${!array[@]}";do printf "\${array[$i]}=\"${array[$i]}\"\n";done
${array[0]}="a"
${array[1]}="b"
${array[2]}="c"
${array[3]}="d"
${array[4]}="e"
# new_string=$(IFS=$'\n';printf "${array[*]}")
# printf "$new_string\n"
a
b
c
d
e
Code: Select all
# another_string=$(printf "%s" "${array[@]/%/\n}")
# printf "$another_string"
a
b
c
d
e
But beware there is a gotcha:
Code: Select all
# yet_another_string=$(IFS=;printf "${array[*]/%/\n}")
# printf "$yet_another_string"
a
b
c
d
e# printf "$yet_another_string"|od -acx
0000000 a nl b nl c nl d nl e nul
a \n b \n c \n d \n e \0
0a61 0a62 0a63 0a64 0065
0000011
Code: Select all
# yet_another_string=$(IFS=;printf "%s" "${array[*]/%/\n}")
# printf "$yet_another_string"|od -acx
0000000 a nl b nl c nl d nl e nl
a \n b \n c \n d \n e \n
0a61 0a62 0a63 0a64 0a65
0000012
Potong