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
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/1943964

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

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


-- 
ubuntu-bugs mailing list
ubuntu-bugs@lists.ubuntu.com
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to