[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-bash] bash variable interpolation
From: |
Dan Douglas |
Subject: |
Re: [Help-bash] bash variable interpolation |
Date: |
Sat, 21 Mar 2015 18:05:02 -0500 |
On Thu, Mar 19, 2015 at 6:37 PM, Peng Yu <address@hidden> wrote:
> The real problem is that I want to replace some bash variables in a
> file and then print the output.
So you want to take the content of a file and expand it as though it were
the content of a heredoc? So in this example FD 3 is your template file and
4 expands the template into the code that generates result.
#!/usr/bin/env bash
x=abc y=$'def\n ghi ' z=\$x
{ eval "$(</dev/fd/4)"; } 3<<\TEMPLATE 4<<-CODE
blah blah $x
blah $y $z
TEMPLATE
{ result=\$(</dev/fd/5); } 5<<EOF
$(</dev/fd/3)
EOF
CODE
printf '%s\n' "$result"
# output:
# blah blah abc
# blah def
# ghi $x
...Provided the template is correct, the result will be correct for any
input variables. I didn't bother fixing trailing newline stripping. You
can fix that if you want.
The only reason I've ever had to do this is for generating complex
tests for testing shell code itself. I can't think of any other good
reason. I wouldn't use this technique merely to abuse a shell
as a macro expander.
--
Dan Douglas
- Re: [Help-bash] bash variable interpolation, (continued)
- Re: [Help-bash] bash variable interpolation, Ken Irving, 2015/03/19
- Re: [Help-bash] bash variable interpolation, John Kearney, 2015/03/20
- Re: [Help-bash] bash variable interpolation, Eduardo A . Bustamante López, 2015/03/19
- Re: [Help-bash] bash variable interpolation, Peng Yu, 2015/03/19
- Re: [Help-bash] bash variable interpolation, Eduardo A . Bustamante López, 2015/03/19
- Re: [Help-bash] bash variable interpolation, Greg Wooledge, 2015/03/20
- Re: [Help-bash] bash variable interpolation, Peng Yu, 2015/03/20
- Re: [Help-bash] bash variable interpolation,
Dan Douglas <=