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).

pevik
  • 1,463
  • 17
  • 28
  • IMHO, local only means "the changes to this variable should not be seen outside of the current scope". – Kusalananda Jul 12 '21 at 07: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. – glenn jackman Jul 12 '21 at 12:50
  • In bash, to set a local valiue initialized to the outer scope value, we'd have to do local foo=$foo – glenn jackman Jul 12 '21 at 12:52

1 Answers1

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.

Kusalananda
  • 333,661