help-bash
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Help-bash] How to let a command take multiple here-doc?


From: Stephane Chazelas
Subject: Re: [Help-bash] How to let a command take multiple here-doc?
Date: Fri, 29 May 2015 10:04:25 +0100
User-agent: Mutt/1.5.21 (2010-09-15)

2015-05-28 22:23:32 -0500, Peng Yu:
> Hi,
> 
> The following bash code uses cat twice. Because one uses EOF the other
> uses 'EOF', they can not readily be combined.
> 
> x='Hello World!'
> cat <<EOF
> $x
> EOF
> cat <<'EOF'
> $x
> EOF
> 
> I can't find that that bash support multiple here-docs for the same
> command. Is it so?
[...]


It does support multiple here-docs, but if you assign those
here-docs to the same file descriptor, the last one will
override the previous ones.

Try:

cat - /dev/fd/3 << EO0 3<< EO3
$x
EO0
$x
EO3

instead.

cat << EOF1 << EOF2
$x
EOF1
$x
EOF2

works in zsh, but that's because of its "multios" feature
whereby when a fd is redicted several times for input, the shell
starts a background process that feeds the  concatenation of
them via a pipe.

When you do

cmd < foo < bar

zsh does the equivalent of:

cmd < <(cat foo bar)

And when you do:

cmd > foo > bar

it does the equivalent of:

cmd > >(tee foo bar)

That's a feature unique to zsh so you won't find it in any other
shell for now. It can be disabled there with unsetopt multios
(it's disabled by default in sh emulation).

-- 
Stephane




reply via email to

[Prev in Thread] Current Thread [Next in Thread]