一個(gè)奇怪的問(wèn)題:
#!/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關(guān)鍵字之后雁刷,就沒(méi)有這個(gè)問(wèn)題了秕重,很奇怪诸典。
之后我在另外一臺(tái)機(jī)器測(cè)試了一下,沒(méi)有這個(gè)問(wèn)題
于是對(duì)比了一下兩臺(tái)機(jī)器bash的版本發(fā)現(xiàn)直奋,
# 有bug的機(jī)器
$bash --version
GNU bash(bdsh), version 3.00.22(2)-release (x86_64-redhat-linux-gnu)
Copyright (C) 2004 Free Software Foundation, Inc.
# 無(wú)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)是版本的問(wèn)題,更新就OK了