On Thu, Oct 08, 2026 at 20:40:12 +0200, Alejandro Colomar wrote:
> I've been experimenting with other forms:
>
> This seems to work:
>
> cat <<-'__EOF__' |
> echo foo;
> __EOF__
> eval "$(sed s/foo/bar/)";
>
> Is the above correct? Or am I abusing any accidents here?
I'm not sure I understand what you're doing here. Near as I can tell,
you've got a here document that you're using as input to a sed one-liner.
The output of the sed one-liner is apparently a shell script that you'd
like to eval. Is that correct?
> This doesn't work:
>
> cat | <<-'__EOF__'
> echo foo;
> __EOF__
> eval "$(sed s/foo/bar/)";
>
> Why does the pipe need to go to the right of the EOF?
The <<-'X' is a single redirection operator. It's applied to the cat
command. The pipe separates the cat command (and its redirections)
from the next command, which in your case is eval.
If you really want to generate a shell script with sed and eval it,
I'd suggest first removing the useless cat:
eval "$(sed s/foo/bar/ <<-'__EOF__'
echo foo
__EOF__
)"
(That's indented with spaces, but you can pretend it's tabs.) At
least, that's how I think the quoting should be arranged. It's very
convoluted, so I'm unsure whether the double quotes work correctly
in that arrangement, especially if there are any double quotes within
the heredoc.
My second suggestion would be to use a function to simplify the quoting.
mkscript() {
sed s/foo/bar/ <<-'__EOF__'
echo foo
__EOF__
}
eval "$(mkscript)"
I'd find that more readable. You could break it down into multiple
functions if that would help -- e.g., one to just cat a here document,
and a second with a much more complex sed or awk filter. I don't
know how much you've simplified your example code.