0

How do I iterate over square brackets type data:

I have written this script:

#!/usr/bin/env bash

# This is a external variable I have in Bash file which has the data already
echo $sample_data_set

for i in "${sample_data_set[@]}"
do
   : 
   echo $i
done

And I need to iterate over $sample_data_set. Am I missing something?

I am getting output:

[ "test1", "test2", "test3", "test4" ]
test.sh: 6: Bad substitution
10
  • assuming sample_data_set is supposed to be an array ... try sample_data_set=( test1 test2 test3 test4 ); the for loop should then work correctly (ie, it should print test1\ntest2\ntest3\ntest4\n Commented Sep 21, 2020 at 15:29
  • I have this output [ "test1", "test2", "test3", "test4" ] coming from separate shell script, which I can't modify. I just provided here to demonstrate that actually Commented Sep 21, 2020 at 15:34
  • 3
    The question is more like "how can I parse a string [ "foo", "bar", "baz" ] with up to 20 values as a Bash array?", right? Commented Sep 21, 2020 at 15:47
  • 1
    See stackoverflow.com/questions/54087481/… for guidance on converting between the two. (Some of those answers are better than others; I'm still looking for something I really like). Commented Sep 21, 2020 at 15:53
  • 1
    ...here we go: stackoverflow.com/questions/62815281/… is on-point. Use the -r argument to jq when what you're extracting are strings and you don't want them quoted as JSON. Commented Sep 21, 2020 at 15:55

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.