0

I have Array of 3 elements and I retrieve these elements in for loop and perform some command on it.

and I want to store the output in single array. How can I do that?

Array1 contains

[ /home/users/abc  /home/users/pqr  /home/users/xyz]

I want to perform

declare -a product_array
IFS=":"
read -ra ADDR <<< "$Array1" 
for i in "${ADDR[@]}"; do
  DIR=${i}/aaa/deployment/traces/    
  product_array=( $(product_array) $(ls $DIR)  )
done

I want to store everything in product_array.

2 Answers 2

1

The syntax

$(product_array)

is completely wrong here; it will try to run product_array as a command and interpolate its output (command substitution). You are looking for

"${product_array[@]}"

which interpolates the array of this name with proper quoting.

Though you should also avoid the undesired or outright dangerous side effects of the ls command; on the whole, you seem to simply want

for dir in "${Array1[@]}"; do
    product_array+=("$dir"/*)
done

You should also avoid upper case for your private variable names; all-uppercase is reserved for system variables.

Sign up to request clarification or add additional context in comments.

Comments

0

I'm still fairly new to arrays in Bash, but I just got this to work without a for loop

a="1 2 3 4"
b="a b c d"
test=($(echo $a))
echo ${test[*]}

1 2 3 4

test+=($(echo $b))
echo ${test[*]}

1 2 3 4 a b c d

1 Comment

That is, however, not what OP was asking.

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.