0

I'll like to convert an array of strings in this format ["1.2.1","1.9.2"] to ("1.2.1" "1.9.2") using a bash script.

I tried this

${MY_TAGS//[[\], .!]}

I got this

'100101'

What I'll like as output in

("1.2.1" "1.9.2")
  • Out of curiosity: what's motivating the transformation? Looks like a JSON literal for the input, not sure of the output. Are you trying to make a list of arguments for a function / script? – Rogue Jul 10 '23 at 13:10
  • 2
    If you have an array of strings, please show how you created it instead of describing it in words. – Ted Lyngmo Jul 10 '23 at 13:10
  • Note that `array=( "one" "two" )` is a way to define a bash array with the literal items `one` and `two` -- the `(`s, the `"`s, &c aren't actually part of the array itself, they're just the definition syntax. Right now we don't know if you want parens and quotes in _output_, or you want an array defined the same way it would be if you had used that syntax. – Charles Duffy Jul 10 '23 at 13:19
  • I'll like to iterate over it. I got it from an az acr repository show-tags command – holyghostgym Jul 10 '23 at 13:20
  • If the actual situation is that you have a JSON array and you want to populate a native bash array from it, that's a different and simpler question (at least, as long as you're willing to use tools like `jq` that understand JSON). – Charles Duffy Jul 10 '23 at 13:20
  • Okay, great -- then you have a JSON array, and you want a bash array; we already have Stack Overflow questions describing how to do that. – Charles Duffy Jul 10 '23 at 13:20
  • In general, that works out to something like `readarray -t my_array < <(az acr repository show-tags | jq -r '.[]')`, after which you can iterate over `"${my_array[@]}"` (assuming bash 4.0 or newer; `readarray` needs to be replaced if you need compatibility with 3.x) – Charles Duffy Jul 10 '23 at 13:22
  • @CharlesDuffy I'll like it in a way that I can easily iterate over it. I'm familiar with the parens and quotes but I'm open to other ways. The goal is for me to be able to iterate over each item at a item because I need to test for some conditions. – holyghostgym Jul 10 '23 at 13:24
  • Right, as I said, `for item in "${my_array[@]}"` will give you that easy iteration. – Charles Duffy Jul 10 '23 at 13:25
  • (you can also do something like `while IFS= read -r item <&3; do ...; done 3< <(az acr repository show-tags | jq -r '.[]')`, putting the code you want to run for each item in the `...` position -- that way you don't need to store content in an array at all, but just iterate over the stream as it comes out of the `az | jq` pipeline; that's also generally how this works when writing for bash 3.x compatibility -- instead of having `readarray` or `mapfile` dump content directly into an array, one uses a `while read` loop and then can append to an array from there). – Charles Duffy Jul 10 '23 at 13:26
  • @CharlesDuffy, Thanks I been able to use the above solution and it works like a charm. I can iterate over all my tags in ACR successfully. Thanks – holyghostgym Jul 10 '23 at 13:57

0 Answers0