GNU bug report logs - #26491
date and echo using with underscore

Previous Next

Package: coreutils;

Reported by: Edmond Yuen <lordedmond <at> gmail.com>

Date: Fri, 14 Apr 2017 06:41:02 UTC

Severity: normal

Tags: notabug

Done: Assaf Gordon <assafgordon <at> gmail.com>

Bug is archived. No further changes may be made.

Full log


View this message in rfc822 format

From: Assaf Gordon <assafgordon <at> gmail.com>
To: Edmond Yuen <lordedmond <at> gmail.com>, 26491 <at> debbugs.gnu.org
Subject: bug#26491: date and echo using with underscore
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






This bug report was last modified 8 years and 123 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.