dash.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* Maybe a mistake
@ 2020-04-17 16:37 lunkov
  2020-04-17 16:51 ` Harald van Dijk
  0 siblings, 1 reply; 2+ messages in thread
From: lunkov @ 2020-04-17 16:37 UTC (permalink / raw)
  To: dash

Hello! I encountered a script execution error using dash. Other shells
execute this script without error. Perhaps there is an error in dash.
Please execute my script in dash and in bash, for example.
-------------------------------------------------------------------------------------------------
#!/bin/dash

DATA='
1
2
${var}
4
5'

test1()
{
    local PATTERN='${var}'

    echo ${DATA} | while read i; do
        F=`echo "${i}" | grep "${PATTERN}"`
        if [ "$F" ]; then
            echo "FOUND"
            break
        fi
    done
}

test2()
{
    local PATTERN='${var}'

    echo ${DATA} | while read i; do
        local F=`echo "${i}" | grep "${PATTERN}"`        # local
        if [ "$F" ]; then
            echo "FOUND"
            break
        fi
    done
}

test3()
{
    local PATTERN='${var}'

    echo "${DATA}" | while read i; do                # quotes
        local F=`echo "${i}" | grep "${PATTERN}"`        # local
        if [ "$F" ]; then
            echo "FOUND"
            break
        fi
    done
}

echo "test1"
test1

echo "test2"
test2    # Only in dash: ./test.sh: 28: local: 2: bad variable name

echo "test3"
test3
-------------------------------------------------------------------------------------------------

Best regards.

^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: Maybe a mistake
  2020-04-17 16:37 Maybe a mistake lunkov
@ 2020-04-17 16:51 ` Harald van Dijk
  0 siblings, 0 replies; 2+ messages in thread
From: Harald van Dijk @ 2020-04-17 16:51 UTC (permalink / raw)
  To: lunkov, dash

On 17/04/2020 17:37, lunkov@gmail.com wrote:
> Hello! I encountered a script execution error using dash. Other shells
> execute this script without error. Perhaps there is an error in dash.
> Please execute my script in dash and in bash, for example.
> -------------------------------------------------------------------------------------------------
[...]
>          local F=`echo "${i}" | grep "${PATTERN}"`        # local

Hi,

The latest release version of dash does not support assignment syntax in 
command arguments. Assignment syntax is special in that it prevents 
field splitting. A shorter example is

   x="1 y=2"
   export x=$x
   echo y=$y

This prints y=2 in the latest release version, but prints y= in other 
shells (unless you had previously set y yourself): without special 
assignment syntax, x=$x is split into x=1 and y=2 before it is passed to 
the export command, but with special assignment syntax x=$x is preserved 
as a single word.

You can work around this by adding quoting:

   local F="`...`"

The current git version of dash does support assignment syntax in 
command arguments (like other shells, only for specific commands) and 
accepts your script without modifications.

Cheers,
Harald van Dijk

^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2020-04-17 16:51 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-04-17 16:37 Maybe a mistake lunkov
2020-04-17 16:51 ` Harald van Dijk

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).