-1

I want to run a for loop for all strings in a variable (one by one). When I echo my variable it is like this

A=(curl -s 'localhost:9200/something)
echo "$A"

emits:

sss
ddd
fff
ggg
hhh

My for loop is like this:

for i in "${A[@]}"; do
  echo "$i"
  echo test
 done

But loop just run one time and print all data, instead of putting test after each item:

sss
ddd
fff
ggg
hhh
test
7
  • Try simple echo $A | while read line; do echo $line; done. Commented Jul 28, 2018 at 6:31
  • @0andriy thanks. it work like this: echo "$A" | while read line; do echo "$line"; done Commented Jul 28, 2018 at 6:47
  • for i in $a; do echo "$i"; echo "test"; done Commented Jul 28, 2018 at 7:03
  • 4
    Better store them in an actual array: a=("sss" "ddd"); for i in "${a[@]}"; do echo "$i"; done Commented Jul 28, 2018 at 8:12
  • 1
    @PesaThe I am running a command and the sending the output to A variable. A=(curl -s 'localhost:9200/something) . the output of this command print strings line by line. how I can send the output to array? Commented Jul 28, 2018 at 8:22

1 Answer 1

1

As suggested by @PresaThe,

while read line ; do  echo "$line" ; echo testsss; done <  <(curl -s 'localhost:9200/something)
Sign up to request clarification or add additional context in comments.

1 Comment

In How to Answer, note the section "Answer Well-Asked Questions", and within that the bullet point regarding questions which "have already been asked and answered many times before".

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.