3

dash shell (unlike bash and busybox ash implementation where both examples print foo: '') takes value of previously defined variable, i.e. guarding with local does not work:

$ foo=ee; bar() { local foo; echo "foo: '$foo'"; }; bar
foo: 'ee'

It requires declare it as empty:

$ foo=ee; bar() { local foo=; echo "foo: '$foo'"; }; bar
foo: ''

Is it a dash bug (worth of reporting) or local expect to initialize variable? The problem with local is that it's not POSIX, although there was an attempt (source which shows how different are local implementations in various shells).

3
  • IMHO, local only means "the changes to this variable should not be seen outside of the current scope".
    – Kusalananda
    Jul 12, 2021 at 7:25
  • 2
    I see this in my dash manpage: "When a variable is made local, it inherits the initial value and exported and readonly flags from the variable with the same name in the surrounding scope, if there is one." -- so, all working as designed. Jul 12, 2021 at 12:50
  • In bash, to set a local valiue initialized to the outer scope value, we'd have to do local foo=$foo Jul 12, 2021 at 12:52

1 Answer 1

2

The behavior that you're observing is documented and what's expected in the dash shell. It is not a bug. From the dash shell's manual:

When a variable is made local, it inherits the initial value and exported and readonly flags from the variable with the same name in the surrounding scope, if there is one. Otherwise, the variable is initially unset.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .