Discussion:
[Help-bash] Bug? Explanation??
Dennis Williamson
2016-12-31 04:10:50 UTC
Permalink
On Dec 30, 2016 11:04 PM, "PePa" <***@passchier.net> wrote:

It seems that when piping into bash, variables have different
'retention' than functions:

r=23; echo $r; echo -e "r=bc\necho $r" |bash
23
23

r(){ echo Hey;}; r; echo -e "r(){ echo Ho;}\nr" |bash
Hey
Ho

Is this a bug, or is there a rationale?
Thanks,
Peter


It's because you have the echo argument inside double quotes and the
variable is expanded before the echo or the piped bash are executed. Change
to single quotes and the expansion will be delayed.
Grisha Levit
2016-12-31 04:07:34 UTC
Permalink
On Fri, Dec 30, 2016 at 11:03 PM, PePa <***@passchier.net> wrote:

r=23; echo $r; echo -e "r=bc\necho $r" |bash

You need to escape the $ in the second echo statement so that $r is not
evaluated to 23 before being echoed.

Chet Ramey
2016-12-31 17:06:00 UTC
Permalink
Post by Dennis Williamson
r=23; echo $r; echo -e "r=bc\necho $r" |bash
You need to escape the |$| in the second echo statement so that |$r| is not
evaluated to |23| before being echoed.
The easiest way to see what bash gets on its standard input is to replace
`bash' in the pipeline with `cat'.
--
``The lyf so short, the craft so long to lerne.'' - Chaucer
``Ars longa, vita brevis'' - Hippocrates
Chet Ramey, UTech, CWRU ***@case.edu http://cnswww.cns.cwru.edu/~chet/
Loading...