0

How to list content of field in bash . My field looks like : array=('+47177372141 '+41753459833' )

 for ((i=0;i<$pn;i++)); do  echo ${pn[${i}]}  done

The problem is that I get amount:2 not the content of the field.

So I want to fix this code to receive : +47177372141 +41753459833

1
  • Your question is unclear. You create an array called array but then attempt to echo data out of an array called pn. Which is correct or are these supposed to be different (and how do they relate)? What is "amount:2" and what is "the content" of the field? Your array assignment also has a syntax problem. It looks like you're missing a closing ' before the space. What do you mean "receive"? Do you want one line of output reading +47177372141 +41753459833?
    – sorpigal
    Mar 22, 2012 at 15:35

2 Answers 2

1

If you have an array named array and a variable pn holding the number of elements in array:

$ array=('+47177372141' '+41753459833' )
$ pn=2
$ for ((i=0;i<$pn;i++)); do  echo ${array[$i]}; done
+47177372141
+41753459833

Another way to iterate over an array (be it numerically indexed or associative) is:

$ for key in "${!array[@]}"; do echo "$key: ${array[$key]}"; done
0: +47177372141
1: +41753459833
0

Presuming that you want your output to look exactly like

+47177372141 +41753459833

and you are storing your data in an array like this

pn=('+47177372141' '+41753459833')

Then the answer is

echo "${pn[@]}"

If you meant something else, please clarify.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.