简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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