Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/bash/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何打印bash数组中的所有元素?_Bash_Terminal - Fatal编程技术网

如何打印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

我在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\u用户没有添加到数组中。

最后一行可能是

printf "%s\n" "${user_list[@]}" >user_list.txt

bash数组仅在使用其名称调用时打印第一个元素。请尝试echo${user_list[@]}。使用echo${myArray[*]}