Subject: bug#26491: date and echo using with underscore

From: Assaf Gordon <[email-masked]>
Date: Fri, 14 Apr 2017 11:50:12 -0400

tag 26491 notabug
close 26491
stop

Hello Edmond,

On 04/14/2017 02:36 AM, Edmond Yuen wrote:
> echo $D_$i

As others have replied (and thanks to all who replied),
an underscore character is valid part of a shell variable,
thus the shell tries to use the content of the variable "D_" - which is
empty.

This is part of the POSIX standard for shell variable expansion.
Specifically here:

http://pubs.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02

   2.6.2 Parameter Expansion
   ...
   "If the parameter name or symbol is not enclosed in braces, the
    expansion shall use the longest valid name (see the Base
    Definitions volume of IEEE Std 1003.1-2001, Section 3.230, Name),
    whether or not the symbol represented by that name exists."

And "Section 3.230, Name" says:
http://pubs.opengroup.org/onlinepubs/009695399/basedefs/xbd_chap03.html#tag_03_230

   "In the shell command language, a word consisting solely of
    underscores, digits, and alphabetics from the portable character
    set."

This is not a bug in date or echo (or a bug at all),
and I'm closing this bug. Discussion can continue by replying to this
thread.

Lastly,
To detect and fail early when undefined variables are used,
you can optionally enable "-u" in your shell scripts:

    #!/bin/sh
    set -u
    A=hello
    echo A=$A
    echo A_=$A_

And the shell will terminate the script with:

    $ ./test.sh
    A=hello
    test.sh: 5: test.sh: A_: parameter not set

regards,
 - assaf