一个奇怪的问题:
#!/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
可以发现 movie和shows输出并不正确,
但是如果去掉local关键字之后,就没有这个问题了,很奇怪。
之后我在另外一台机器测试了一下,没有这个问题
于是对比了一下两台机器bash的版本发现,
# 有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.
最终发现是版本的问题,更新就OK了
网友评论