bash - Performing variable substitution in a string -


i have string contains use of variable, , i'd substitute variable's value string. right best have is

#!/bin/bash  foo='$name; echo bar' name="the    name" expanded="$(eval echo "$foo")" echo "$expanded" 

which has obvious defects: prints

the name bar 

while i'd print

the    name; echo bar 

instead of eval can bash's regex matching bash's string replacement:

foo='$name; echo bar' name="the    name" [[ "$foo" =~ \$([[:alnum:]]+) ]] && s="${!bash_rematch[1]}" &&   expanded="${foo/\$${bash_rematch[1]}/$s}"  echo "$expanded"    name; echo bar 

Comments

Popular posts from this blog

Payment information shows nothing in one page checkout page magento -

tcpdump - How to check if server received packet (acknowledged) -