在外壳程序脚本中转义星号

时间:2018-07-26 14:27:10

标签: bash

下面的数组中的星号在分配给数组时会扩展到文件列表中。

u='*','john'
IFS=$',';q=($u)
for j in "${!q[@]}"
do
        echo "drop user ${q[j]}"
done

输出为:

drop user abc
drop user test.sh
drop user test1.sh
drop user john

我打算得到的是:

drop user *
drop user john

我如何摆脱星号?

1 个答案:

答案 0 :(得分:3)

您可以使用以下脚本:

u='*,john'

# read comma delimited string into an array
IFS=, read -ra q <<< "$u"

# check content of array q
declare -p q

# loop through array q
for j in "${q[@]}"
do
    echo "drop user $j"
done

输出:

declare -a q=([0]="*" [1]="john")
drop user *
drop user john