简体   繁体   中英

How do I expand variables in a bash variable without expanding wildcard?

I have a variable that contains this kind of string :

var='$FOO/bar/baz*'

and I want to replace the variable $FOO by its content. However, when i do

var=$(eval "echo $var")

The variable is replaced, but the star is also replaced so that var now contains every possible match in my filesystem (as if i pressed tab in a shell). for example, if $FOO contains /home, var will contain "/home/bar/baz1.sh /home/bar/baz2.sh /home/bar/baz.conf"

How do i replace the variable without expanding wildcards ?

Turn off globbing in bash, then reenable it.

set -f 
var="$FOO/bar/baz*"
set +f

Just drop the quotes:

var=$FOO/bar/baz/*

Globs are not expanded on the RHS of a variable assignment.

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