bug-bash
[Top][All Lists]
Advanced

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

Possible bug with respect to global variable within block of code when p


From: Thiemo Kellner
Subject: Possible bug with respect to global variable within block of code when piping the output of later
Date: Wed, 26 Nov 2008 20:23:23 +0100
User-agent: Internet Messaging Program (IMP) H3 (4.1.6)


Hi

I stumbled over that variables defined outside of blocks of code do not get altered by assignment statements to that variable within the block when the output of the block is piped to some other process. Re-direction of the block output to a file or no re-direction changes the outer variable. It seems to me inconsistent behavior and might be regarded as bug. Maybe the piping converts the block implicitly to a subshell. Please find below a demo script.

Version of my bash:
GNU bash, version 3.00.16(1)-release (sparc-sun-solaris2.10)
Copyright (C) 2004 Free Software Foundation, Inc.

#!/bin/bash

# Block changes outer variable
_TEST_VARIABLE=5
echo "_TEST_VARIABLE out of block: ${_TEST_VARIABLE}"  # 5
{
   echo "_TEST_VARIABLE in block: ${_TEST_VARIABLE}"   # 5
   _TEST_VARIABLE=asedfawsed
   echo "_TEST_VARIABLE in block: ${_TEST_VARIABLE}"   # asedfawsed
}
echo "_TEST_VARIABLE out of block: ${_TEST_VARIABLE}"  # asedfawsed

echo

# Block does not change outer variable when output piped
_TEST_VARIABLE=5
echo "_TEST_VARIABLE out of block: ${_TEST_VARIABLE}"  # 5
{
   echo "_TEST_VARIABLE in block: ${_TEST_VARIABLE}"   # 5
   _TEST_VARIABLE=asedfawsed
   echo "_TEST_VARIABLE in block: ${_TEST_VARIABLE}"   # asedfawsed
} | grep -E "."
echo "_TEST_VARIABLE out of block: ${_TEST_VARIABLE}"  # 5


echo

# Block changes outer variable when output piped
_TEST_VARIABLE=5
echo "_TEST_VARIABLE out of block: ${_TEST_VARIABLE}"  # 5
{
   echo "_TEST_VARIABLE in block: ${_TEST_VARIABLE}"   # 5
   _TEST_VARIABLE=asedfawsed
   echo "_TEST_VARIABLE in block: ${_TEST_VARIABLE}"   # asedfawsed
} > tmp.txt

cat tmp.txt
echo "_TEST_VARIABLE out of block: ${_TEST_VARIABLE}"  # asedfawsed


exit 0



Cheers

Thiemo





reply via email to

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