+1 vote
in Operating Systems by (88.8k points)

In bash script, I want to create backup for 3 users. I have declared an array x_users as follows and have used the for loop to iterate over all 3 users:

x_users=('user_1', 'user_2', 'user_3')

for user in "${x_users[@]}"; do

However, it creates backup only for user_3. Is there anything wrong in the syntax of declaration of array?

1 Answer

+2 votes
by (362k points)
selected by
 
Best answer

Yes, there is a syntax issue in your array declaration. In Bash, arrays should not have commas between elements. Instead, they should be space-separated. Here’s the corrected version:

x_users=('user_1' 'user_2' 'user_3')
for user in "${x_users[@]}"; do

This should now loop through all three users correctly.


...