1

I have an array of customer names that I want to use to make a directory. Below is the code I'm running:

$ echo "$customerArray=( customer1 customer2 customer3 customer4 customer5 )"

for customerName in $( customerArray ); 
do
    mkdir -p /home/$customerName
    mkdir -p /home/$customerName/outbound
    mkdir -p /home/$customerName/outbound_backup
    mkdir -p /home/$customerName/dropoff
done

Can anyone explain to me what I'm doing wrong?

0

2 Answers 2

3

You cannot set a variable inside of a string literal and I'm pretty sure that $( customerArray ) is invalid as well. Try the following instead:

customerArray=(customer1 customer2 customer3 customer4 customer5)
for customerName in ${customerArray[@]}
do
    ...
done

You need to reference an array variable as either ${customerArray[@]} or ${customerArray[*]}. The bash manual describes this in more depth.

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

Comments

3

This would be a working alternative using brace expansion

customerArray=(customer1 customer2 customer3 customer4 customer5)
for customerName in ${customerArray[*]}
do
  mkdir -p /home/$customerName/{outbound,outbound_backup,dropoff}
done

1 Comment

Hey Steven thanks for the response. I am actually more so trying to test whether or not the for loop I specify above is properly reading in the information from the array.

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.