如何打印bash数组中的所有元素?
我在bash中运行这个脚本,将用户列表添加到数组中,然后将其写入文件。以下是脚本:如何打印bash数组中的所有元素?,bash,terminal,Bash,Terminal,我在bash中运行这个脚本,将用户列表添加到数组中,然后将其写入文件。以下是脚本: 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 [ $an
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;
除了数组只包含第一个元素之外,一切都正常。我不明白为什么$new\u用户没有添加到数组中。最后一行可能是
printf "%s\n" "${user_list[@]}" >user_list.txt
bash数组仅在使用其名称调用时打印第一个元素。请尝试echo${user_list[@]}。使用echo${myArray[*]}