Home > Blockchain >  Converting bash string variable to quotes separated words
Converting bash string variable to quotes separated words

Time:04-07

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

CodePudding user response:

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//,/','}'"`.

  •  Tags:  
  • bash
  • Related