I'm trying to write a script that finds strings in a certain pattern ({?varname}
) and replaces them with a corresponding environment variable.
This is what I have so far:
function get_env() { var=$1 echo ${!var} } sed -e 's/{?\([a-z]*}\)/'$(get_env '\1')'/g' file.txt
The function works, e.g. get_env LOGNAME
-->dotan
Sed returns the value of the function, e.g. if I replace the function's content with echo __$1__
I will get {?logname}
-->__LOGNAME__
However put together it doesn't work. It's like the function always returns an empty string.
I'm not sure what causes the problem here. Any ideas?
echo "${!var}"
\1
out ofsed
, back into the shell and into your function, and then get the correct value back from there.