bash从字符串中解析出带引号的变量

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#!/bin/bash
#shebang must be bash.
 
print_args()
{
	echo "1st is [$1]"
	echo "2nd is [$2]"
	echo "3rd is [$3]"
	echo "4th is [$4]"
	echo "5th is [$5]"
	echo "6th is [$6]"
	echo "7th is [$7]"
	echo "8th is [$8]"
}
 
VAR1=(1 2 "3 4" 5)
print_args "${VAR1[@]}"
 
VAR2="11 22 \"33 44\" 55"
#eval "VAR2=($VAR2)"
declare -a "VAR2=($(echo $VAR2 | tr '`$<>' '????'))"
print_args "${VAR2[@]}"

refer to:
https://superuser.com/questions/1066455/how-to-split-a-string-with-quotes-like-command-arguments-in-bash