Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
我正在 bash 中尝试这段小代码:
echo "Enter number(a b c)" read natc echo ${natc[1]}
输入应该是一行中的 3 个数字,空格分隔,从标准输入读取,例如4 5 6 ,我想在数组中访问它们,natc[]即natc[0]=4, natc[1]=5等。
4 5 6
natc[]
natc[0]=4, natc[1]=5
google 中有很多示例,但主要是从文件或预定义值中读取数组。我怎样才能实现我的目标?
你可以读入一个数组-a:
-a
read -a natc
使用-p和-a阅读选项:
-p
read -p "Enter an array: " -a natc Enter an array: a b c d echo "${natc[0]}" a echo "${natc[1]}" b echo "${natc[2]}" c echo "${natc[3]}" d echo "${#natc[@]}" 4