<?xml version="1.0" encoding="UTF-8" ?>

<bugzilla version="5.2"
          urlbase="https://bugzilla.altlinux.org/"
          
          maintainer="jenya@basealt.ru"
>

    <bug>
          <bug_id>37272</bug_id>
          
          <creation_ts>2019-09-28 12:50:19 +0300</creation_ts>
          <short_desc>Определение переменной для конкретной функции перезаписывает значение глобально</short_desc>
          <delta_ts>2023-11-11 18:35:37 +0300</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>4</classification_id>
          <classification>Development</classification>
          <product>Sisyphus</product>
          <component>sh4</component>
          <version>unstable</version>
          <rep_platform>all</rep_platform>
          <op_sys>Linux</op_sys>
          <bug_status>NEW</bug_status>
          <resolution></resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords></keywords>
          <priority>P3</priority>
          <bug_severity>normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Vitaly Lipatov">lav</reporter>
          <assigned_to name="placeholder@altlinux.org">placeholder</assigned_to>
          <cc>cas</cc>
    
    <cc>evg</cc>
    
    <cc>glebfm</cc>
    
    <cc>iv</cc>
    
    <cc>ldv</cc>
    
    <cc>mike</cc>
    
    <cc>placeholder</cc>
          
          <qa_contact>qa-sisyphus</qa_contact>

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>184630</commentid>
    <comment_count>0</comment_count>
    <who name="Vitaly Lipatov">lav</who>
    <bug_when>2019-09-28 12:50:19 +0300</bug_when>
    <thetext>По неожидаемой таинственной причине переопределение переменной при вызове функции из shell переопределяет переменную глобально, если использовать sh, а не bash в качестве интерпретатора.

$ ssh builder-p8 sh ./z.sh
ALTLinux/p8
 $ rpm -qf /bin/sh
sh-3.2.57-alt1
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: REPLACED after func

$ ssh builder-p8 bash ./z.sh
ALTLinux/p8
 $ rpm -qf /bin/bash
bash-3.2.57-alt1
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: ORIGINAL

$ ssh builder-p9 sh ./z.sh
ALTLinux/p8
 $ rpm -qf /bin/sh
sh-4.4.23-alt1.noarch
Note: /bin/sh is link to /bin/sh4
 $ rpm -qf /bin/sh4
sh4-4.4.23-alt1.x86_64
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: REPLACED after func

$ ssh builder-p9 bash ./z.sh
ALTLinux/p8
 $ rpm -qf /bin/bash
bash-4.4.23-alt1.noarch
Note: /bin/bash is link to /bin/bash4
 $ rpm -qf /bin/bash4
bash4-4.4.23-alt1.x86_64
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: ORIGINAL

$ ssh builder64 sh ./z.sh
ALTLinux/Sisyphus
 $ rpm -qf /bin/sh
sh-4.4.23-alt1.noarch
Note: /bin/sh is link to /bin/sh4
 $ rpm -qf /bin/sh4
sh4-4.4.23-alt1.x86_64
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: REPLACED after func

$ ssh builder64 bash ./z.sh
ALTLinux/Sisyphus
 $ rpm -qf /bin/bash
bash-4.4.23-alt1.noarch
Note: /bin/bash is link to /bin/bash4
 $ rpm -qf /bin/bash4
bash4-4.4.23-alt1.x86_64
TESTIK 1: ORIGINAL
TESTIK 2: ORIGINAL
TESTIK 3: REPLACED after func
TESTIK 4: ORIGINAL


$ cat z.sh
#!/bin/sh

test_func()
{
    echo &quot;TESTIK $1: $testik&quot;
}

distr_info
epmqf /bin/sh
epmqf /bin/bash

testik=&quot;ORIGINAL&quot;
test_func 1
testik=&apos;REPLACED after command&apos; /bin/true
test_func 2
testik=&apos;REPLACED after func&apos; test_func 3
test_func 4
x</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>184634</commentid>
    <comment_count>1</comment_count>
    <who name="Dmitry V. Levin">ldv</who>
    <bug_when>2019-09-28 14:58:37 +0300</bug_when>
    <thetext>$ sh -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
4.4.23(1)-release
a=1
a=2
a=2

$ bash -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
4.4.23(1)-release
a=1
a=2
a=1</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>185009</commentid>
    <comment_count>2</comment_count>
    <who name="Vitaly Lipatov">lav</who>
    <bug_when>2019-10-19 18:01:22 +0300</bug_when>
    <thetext>А можно узнать, какие планы по исправлению?
И что делать? Искать проблему самому, переходить на использование bash?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>185334</commentid>
    <comment_count>3</comment_count>
    <who name="Vitaly Lipatov">lav</who>
    <bug_when>2019-11-05 21:30:27 +0300</bug_when>
    <thetext>По моим сведениям, проблему вызывает использование
--disable-restricted
при сборке sh</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>185335</commentid>
    <comment_count>4</comment_count>
    <who name="Vitaly Lipatov">lav</who>
    <bug_when>2019-11-05 22:17:23 +0300</bug_when>
    <thetext>(В ответ на комментарий №3)
&gt; По моим сведениям, проблему вызывает использование
&gt; --disable-restricted
&gt; при сборке sh
Прошу прощения, проблему вызывает отличие в названии файла:

[lav@builder64 build-sh]$ md5sum sh4 bash
222e0437b72384f06b5182dec89615ac  sh4
222e0437b72384f06b5182dec89615ac  bash
[lav@builder64 build-sh]$ ./bash -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
4.4.23(1)-release
a=1
a=2
a=1
[lav@builder64 build-sh]$ ./sh4 -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
4.4.23(1)-release
a=1
a=2
a=2</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>185354</commentid>
    <comment_count>5</comment_count>
    <who name="Ivan A. Melnikov">iv</who>
    <bug_when>2019-11-06 11:39:26 +0300</bug_when>
    <thetext>Хотелось бы отметить следующее:

$ sh3 -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
3.2.57(1)-release
a=1
a=2
a=2
$ bash3 -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
3.2.57(1)-release
a=1
a=2
a=1
$ dash -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;

a=1
a=2
a=2
$ rpm -qf `which dash`
ash-0.5.8-alt1.2e5842258.x86_64</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>236899</commentid>
    <comment_count>6</comment_count>
    <who name="Vitaly Lipatov">lav</who>
    <bug_when>2023-11-11 18:35:37 +0300</bug_when>
    <thetext>(Ответ для Dmitry V. Levin на комментарий #1)
&gt; $ sh -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
&gt; 4.4.23(1)-release
&gt; a=1
&gt; a=2
&gt; a=2
&gt; 
&gt; $ bash -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
&gt; 4.4.23(1)-release
&gt; a=1
&gt; a=2
&gt; a=1

Для bash5 разницы больше нет:

[lav@builder64 beeheave]$ sh -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
5.2.15(1)-release
a=1
a=2
a=1
[lav@builder64 beeheave]$ bash -c &apos;foo(){ echo &quot;a=$a&quot;; }; echo $BASH_VERSION; a=1; foo; a=2 foo; foo&apos;
5.2.15(1)-release
a=1
a=2
a=1</thetext>
  </long_desc>
      
      

    </bug>

</bugzilla>