Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

bash

eval

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 ?

like image 666
Kaidjin Avatar asked Apr 02 '13 15:04

Kaidjin


2 Answers

Turn off globbing in bash, then reenable it.

set -f 
var="$FOO/bar/baz*"
set +f
like image 171
jim mcnamara Avatar answered Oct 25 '22 05:10

jim mcnamara


Just drop the quotes:

var=$FOO/bar/baz/*

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

like image 33
chepner Avatar answered Oct 25 '22 03:10

chepner