我想用while read
這個數組,但整個數組一次輸出。while read bash array(not into)
#!/bin/bash
declare -a arr=('1:one' '2:two' '3:three');
while read -e it ; do
echo $it
done <<< ${arr[@]}
它應該分別輸出每個值(但沒有),所以也許在閱讀不是熱門票嗎?
我想用while read
這個數組,但整個數組一次輸出。while read bash array(not into)
#!/bin/bash
declare -a arr=('1:one' '2:two' '3:three');
while read -e it ; do
echo $it
done <<< ${arr[@]}
它應該分別輸出每個值(但沒有),所以也許在閱讀不是熱門票嗎?
對於這種情況,很容易使用for
循環:
$ declare -a arr=('1:one' '2:two' '3:three')
$ for it in "${arr[@]}"; do echo $it; done
1:one
2:two
3:three
的while read
方法是非常有用的(a)如果您想從文件中讀取數據,以及(b)當你想以nul或換行符分隔字符串讀取。但是,對於您的情況,您已經擁有bash
變量中的數據,並且for
循環更簡單。
可能通過while循環
#!/bin/bash
declare -a arr=('1:one' '2:two' '3:three');
len=${#arr[@]}
i=0
while [ $i -lt $len ]; do
echo "${arr[$i]}"
let i++
done
'貓<<< $ {ARR [@]}'將所有元件在同一直線上。 –
@couling:我也嘗試過'<和'<<',但沒有真正明白。 –