1. 正确的方式
参考:https://stackoverflow.com/questions/52184686/dynamic-variable-assignment-in-shell-script
for ((i=1;i<=${#};i++)) ; do
option="${*:${i}:1}"
echo "arg: [${option}]"
done
使用了 shell
语法 ${parameter:offset:length}
$*
is a special variable which expands to the list of positional parameters (arguments to your script), separated by a space character.${list: -1}
expands to the last element of a list.
2. 错误的方式
for a in ${@} ; do # 或者 for a in $*
echo "${a}"
done
这种方式的错误在于,会把所有参数搞成一行,然后以空格作为分隔符进行拆分。这就回导致 "select count(*) from a"
这个被双引号包起来的语句被分割成了4个参数。