Do you know how I could use a backreference as a variable name, in sed?
What I tried:
#!/bin/bash
var_one=x
var_two=y
var_three=z
echo "var_one + var_two = var_three" | sed -e "s/\(var_[^\ ]*\)/$\1/g"
What I got:
$var_one + $var_two = $var_three
What I want to get:
x + y = z
Expand Variables with Eval
It’s not clear what you’re really trying to do, but if all you want to do is create some variables in a string, and then evaluate the string to expand the variables, then you want to use eval. For example:
There are a lot of pitfalls involved with using eval. However, this code certainly solves the problem you’ve described.