Bash pipe syntax makes variables local, breaking sendfax scripts

Chet Ramey (chet@odin.INS.CWRU.Edu)
Fri, 19 Jan 1996 16:20:01 +0100


> Look at this shell dialogue:
> 
> <dialog(>
> 
> $ unset E;{ frazzle;E=$? }|tee dum;echo errorlevel $E
> unset E;{ frazzle;E=$? };echo errorlevel $E
> bash: frazzle: command not found
> errorlevel 127

This is wrong; I'm sure you meant to omit the `|tee dum'.

> $ unset E;{ frazzle;E=$? }|tee dum;echo errorlevel $E
> unset E;{ frazzle;E=$? }|tee dum;echo errorlevel $E
> bash: frazzle: command not found
> errorlevel
> $ unset E;{ frazzle;E=$? }|tee dum;echo errorlevels $E and $?
> unset E;{ frazzle;E=$? }|tee dum;echo errorlevels $E and $?
> bash: frazzle: command not found
> errorlevels and 0
> 
> <)dialog>
> 
> It becomes clear that the `tee' pipe prevents the 
> declaration of E from being valid outside the curled bracket.

Each component of a pipeline is run in a subshell.  Variables set
in the subshell are not available to the parent.

> The tee pipe unfortunately also changes the errorlevel of
> everything to 0.  A desirable Bash behavior would be to
> end with the first nonzero error code of the pipe chain.

That's not how pipelines work.  The exit status of a pipe is the
exit status of the last command.  Thus the exit status of `tee'
is what determines the exit status of the entire pipeline.

-- 
``The lyf so short, the craft so long to lerne.'' - Chaucer

Chet Ramey, Case Western Reserve University	Internet: chet@po.CWRU.Edu