[Bug 1943964] [NEW] local var="$@" doesn't behave like every other system

raf 1943964 at bugs.launchpad.net
Fri Sep 17 12:05:56 UTC 2021


Public bug reported:

With Ubuntu (for many years), the following code in /bin/sh (i.e. dash):

  f()
  {
    local v="$@"
    echo "<$v>"
  }
  f "$@"

behaves differently to /bin/sh on Debian, macOS, FreeBSD, Solaris (/usr/xpg4/bin/sh).
The behaviour of "$@" is very special. When there are no arguments, it completely
disappears (i.e. even the surrounding quotes disappear so that there are no words). When
there are arguments, it replicates those arguments perfectly (i.e. it effectively produces
multiple quoted arguments if necessary).

The described behaviour applies when "$@" is used for local variable assignment on Ubuntu,
which sounds consistent with other uses of "$@" and therefore correct, but it doesn't
match the behaviour on any other system, and is therefore a problem for shell portability.
On the other systems mentioned above, the behaviour of "$@" when used as the value of a
variable assignment (local or not) is as though "$*" had been used (i.e. it produces a
single argument containing all the words).

The difference is clear when the arguments contain multiple words. On Ubuntu, only
the first word is assigned to v. Any subsequent words are treated as subsequent variable
names. This goes unnoticed if the subsequent words are valid variable identifiers.
But if the subsequent words are not valid identifiers, then dash emits a "bad variable name"
error message.

Note that without the "local" keyword, "$@" does behave like "$*" (like
all the other systems).

You might think that this is correct behaviour, but it is contrary to non-local variable
assignment, and contrary to all other systems, and is therefore unhelpful. Please change
it to behave like dash on other systems, e.g. debian.

This behaviour has been witnessed on ubuntu-16.x and ubuntu-20.04.

** Affects: dash (Ubuntu)
     Importance: Undecided
         Status: New

-- 
You received this bug notification because you are a member of Ubuntu
Foundations Bugs, which is subscribed to dash in Ubuntu.
https://bugs.launchpad.net/bugs/1943964

Title:
  local var="$@" doesn't behave like every other system

Status in dash package in Ubuntu:
  New

Bug description:
  With Ubuntu (for many years), the following code in /bin/sh (i.e.
  dash):

    f()
    {
      local v="$@"
      echo "<$v>"
    }
    f "$@"

  behaves differently to /bin/sh on Debian, macOS, FreeBSD, Solaris (/usr/xpg4/bin/sh).
  The behaviour of "$@" is very special. When there are no arguments, it completely
  disappears (i.e. even the surrounding quotes disappear so that there are no words). When
  there are arguments, it replicates those arguments perfectly (i.e. it effectively produces
  multiple quoted arguments if necessary).

  The described behaviour applies when "$@" is used for local variable assignment on Ubuntu,
  which sounds consistent with other uses of "$@" and therefore correct, but it doesn't
  match the behaviour on any other system, and is therefore a problem for shell portability.
  On the other systems mentioned above, the behaviour of "$@" when used as the value of a
  variable assignment (local or not) is as though "$*" had been used (i.e. it produces a
  single argument containing all the words).

  The difference is clear when the arguments contain multiple words. On Ubuntu, only
  the first word is assigned to v. Any subsequent words are treated as subsequent variable
  names. This goes unnoticed if the subsequent words are valid variable identifiers.
  But if the subsequent words are not valid identifiers, then dash emits a "bad variable name"
  error message.

  Note that without the "local" keyword, "$@" does behave like "$*"
  (like all the other systems).

  You might think that this is correct behaviour, but it is contrary to non-local variable
  assignment, and contrary to all other systems, and is therefore unhelpful. Please change
  it to behave like dash on other systems, e.g. debian.

  This behaviour has been witnessed on ubuntu-16.x and ubuntu-20.04.

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/dash/+bug/1943964/+subscriptions




More information about the foundations-bugs mailing list