Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Function scoped variable should retain its last value in while-loop body #1476

Open
earlchew opened this issue Apr 9, 2020 · 3 comments
Open

Comments

@earlchew
Copy link

earlchew commented Apr 9, 2020

Description of problem:
When a function scoped variable is set in the body of a while loop, the variable appears to lose the value when the loop is restarted.

Ksh version:

# print -v .sh.version
Version AJM 93u+ 2012-08-01

How reproducible:
Easily reproduced from console. The output from dash, and bash, is less surprising:

$ dash /tmp/foo.sh | head -4
/tmp/foo.sh: 11: print: not found
/tmp/foo.sh: 13: typeset: not found

+
+
+

Steps to reproduce:

$ cat /tmp/foo.sh
f()
{
    typeset X

    while : ; do
        echo "${X++}"
        X=1
    done
}

print -v .sh.version

f

$ ksh /tmp/bug.sh | head
Version AJM 93u+ 2012-08-01









```
@marcastel
Copy link

Using an alternate value in KornShell is not ${parameter+word} but {parameter:+word}.

The following should achieve what you are expecting (both in KornShell and Bash).

    while : ; do
        echo "${X:++}"
        X=1
    done | head

That is an empty line followed by 9 lines with 1 x -- assuming the default for head(1) on your platform is 10.

BTW. Please read also @jghub's comment in #1475 about this repo.

@earlchew
Copy link
Author

earlchew commented Apr 9, 2020

@marcastel Thanks for the feedback, and I appreciate the suggested workaround.

@jelmd
Copy link

jelmd commented Apr 10, 2020

Well, your script works as expected: you set X=1 and echo the contents of another var named X++. Since you never set the value of this var, it is empty - you get, what you are asking for.
dash is obviously buggy wrt. the output of the script.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants