'Converting bash string variable to quotes separated words

I have a string variable in bash

$ t_partitions='p0,p1,p2'

I need to convert this string variable into

$ t_partitions=''p0','p1','p2''

Can someone help

Here is my attempted solution

          t_partitions="p0,p1,p2"
          new="'"

          for (( i=0; i<${#t_partitions}; i++ )); do

          if[${t_partitions:$i:1}==",”];
          then
          $new+="'"
          $new+="${t_partitions:$i:1}” 
          $new+="'"

          else
          $new+="${t_partitions:$i:1}”
          fi

          done
          $t_partitions=$new


Solution 1:[1]

You may do this in bash:

t_partitions='p0,p1,p2'
t_partitions="'${t_partitions//,/\',\'}'"
echo "$t_partitions"

'p0','p1','p2'

Here ${t_partitions//,/\',\'} replaces each comma with ',' and we wrap value with ' at start and end positions using "'${t_partitions//,/','}'"`.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1