简体   繁体   English

在shell脚本中转​​义星号

[英]Escape asterisk in shell script

The asterisk sign in the below array is getting expanded into the list of files when assigning it to an array. 下面的数组中的星号符号在分配给数组时会扩展到文件列表中。

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

The output is: 输出是:

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

What I intend to get is: 我打算得到的是:

drop user *
drop user john

How can I escape the asterisk? 我怎么能逃过星号?

You may use this script: 您可以使用此脚本:

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

Output: 输出:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM