0

I am running this script in bash to add a list of user to an array and then write it to a file. This is the script:

echo "Insert the first user of the list"
read -r user_name
user_list=()
echo "User $user_name inserted!"
user_list+=($user_name)

echo "Do you want to insert another user?(yes or no)"
read -r answ
while [ $answ == "yes" ]; do
     echo "Enter a new user to insert"
     read -r new_user
     user_list+=($new_user)
     echo "Users list contains:"
     echo "$user_list"
     echo "Do you want to add another user?(yes or no)"
     read -r answ
done

echo "$user_list" > user_list.txt;

Everything works fine except that the array only contains the first element.I don't understand why the $new_user are not added to the array.

jeremysprofile
  • 10,028
  • 4
  • 33
  • 53
Marco
  • 1,195
  • 3
  • 18
  • 30

1 Answers1

1

Last line could be

printf "%s\n" "${user_list[@]}" >user_list.txt
F. Hauri - Give Up GitHub
  • 64,122
  • 17
  • 116
  • 137