【发布时间】:2019-04-01 15:07:10
【问题描述】:
我在 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 [ $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_user 没有添加到数组中。
【问题讨论】:
-
bash 数组仅在使用其名称调用时打印第一个元素。
-
试试
echo "${user_list[@]}"。 -
使用
echo "${myArray[*]}"