一個奇怪的問題:
#!/bin/bash
set -x
function run {
local table_arr=(
"movie"
"tvplay"
"comic"
"shows"
)
for val in ${table_arr[@]}; do
echo $val
done
}
run
echo ${table_arr[1]}
最終輸出
+ run
+ local 'table_arr=(movie tvplay comic shows)'
+ for val in '${table_arr[@]}'
+ echo '(movie'
(movie
+ for val in '${table_arr[@]}'
+ echo tvplay
tvplay
+ for val in '${table_arr[@]}'
+ echo comic
comic
+ for val in '${table_arr[@]}'
+ echo 'shows)'
shows)
+ echo
可以發(fā)現(xiàn) movie和shows輸出并不正確,
但是如果去掉local關鍵字之后,就沒有這個問題了,很奇怪。
之后我在另外一臺機器測試了一下,沒有這個問題
于是對比了一下兩臺機器bash的版本發(fā)現(xiàn),
# 有bug的機器
$bash --version
GNU bash(bdsh), version 3.00.22(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
# 無bug的
$ bash --version
GNU bash(bdsh), version 4.1.17(2)-release (x86_64-unknown-linux-gnu)
Copyright (C) 2009 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software; you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
最終發(fā)現(xiàn)是版本的問題,更新就OK了