[Top][All Lists]

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

Re: using tee after if-fi construct clobbers variable

From: Eric Blake
Subject: Re: using tee after if-fi construct clobbers variable
Date: Mon, 22 Jan 2018 10:49:24 -0600
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.5.2

On 01/22/2018 10:00 AM, Steve Aberle wrote:

> #/bin/sh
> # Example:    use of tee following an if-then-else-fi construct
> #        clobbers a variable set within that construct.

Not a problem of tee, but a limitation of your shell.

> # tee into a file (after if-then-else-fi construct) - CLOBBERS VARIABLE
> VAR_3="none"
> if [ "$VAR_3" == "none" ]
>   then
>     VAR_3="true"
>     echo -e "Location 7:\t\$VAR_3 = \"$VAR_3\""
>   else
>     VAR_3="false"
>     echo -e "Location 8:\t\$VAR_3 = \"$VAR_3\""
>   fi    | tee $LOG3

POSIX says that the use of a pipeline can (but not must) create a
subshell for the elements of the pipeline; once you have a subshell,
changes made in that shell are NOT visible to the parent shell.  It is
not tee that is clobbering variables, but the fact that your shell chose
to use a subshell to implement the pipeline.

There's nothing coreutils can do about your shell, so you may be better
off redirecting your question to a forum on shell programming. has more details about this
common shell programming pitfall.

Eric Blake, Principal Software Engineer
Red Hat, Inc.           +1-919-301-3266
Virtualization: |

Attachment: signature.asc
Description: OpenPGP digital signature

reply via email to

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