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.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 26491 in the body.
You can then email your comments to 26491 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-coreutils <at> gnu.org:
bug#26491; Package coreutils. (Fri, 14 Apr 2017 06:41:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Edmond Yuen <lordedmond <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-coreutils <at> gnu.org. (Fri, 14 Apr 2017 06:41:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Edmond Yuen <lordedmond <at> gmail.com>
To: bug-coreutils <at> gnu.org
Subject: date and echo using with underscore
Date: Fri, 14 Apr 2017 14:36:49 +0800
[Message part 1 (text/plain, inline)]
Please verify the script below:

#/bin/bash

D=`date +%Y%m%d`
i="someparameter"

echo $D_$i
echo $D\_$i
echo $D
echo $i
echo _




//   my system is debian with coreutils 8.26-3
//
//   db=`date -d "-1day" +%Y%m%d`
//   i="0"
This is the result from my system

~$ echo "$db_$i"
0
~$ echo "$db\_$i"
20170413\_0
~$ echo "$db$i"
201704130

Thank you!
[Message part 2 (text/html, inline)]

Information forwarded to bug-coreutils <at> gnu.org:
bug#26491; Package coreutils. (Fri, 14 Apr 2017 15:15:02 GMT) Full text and rfc822 format available.

Message #8 received at 26491 <at> debbugs.gnu.org (full text, mbox):

From: Reuti <reuti <at> staff.uni-marburg.de>
To: Edmond Yuen <lordedmond <at> gmail.com>
Cc: 26491 <at> debbugs.gnu.org
Subject: Re: bug#26491: date and echo using with underscore
Date: Fri, 14 Apr 2017 17:14:07 +0200
Hi,

> Am 14.04.2017 um 08:36 schrieb Edmond Yuen <lordedmond <at> gmail.com>:
> 
> Please verify the script below:
> 
> #/bin/bash
> 
> D=`date +%Y%m%d`
> i="someparameter"
> 
> echo $D_$i
> echo $D\_$i
> echo $D
> echo $i
> echo _
> 
> 
> 
> 
> //   my system is debian with coreutils 8.26-3
> //
> //   db=`date -d "-1day" +%Y%m%d`
> //   i="0"
> This is the result from my system
> 
> ~$ echo "$db_$i"
> 0
> ~$ echo "$db\_$i"
> 20170413\_0
> ~$ echo "$db$i"
> 201704130

The syntax is to use curly braces to limit the name of the varable:

echo "${db}_${i}"

-- Reuti




Information forwarded to bug-coreutils <at> gnu.org:
bug#26491; Package coreutils. (Fri, 14 Apr 2017 15:29:02 GMT) Full text and rfc822 format available.

Message #11 received at 26491 <at> debbugs.gnu.org (full text, mbox):

From: Martin Møller Skarbiniks Pedersen <traxplayer <at> gmail.com>
To: Edmond Yuen <lordedmond <at> gmail.com>
Cc: 26491 <at> debbugs.gnu.org
Subject: Re: bug#26491: date and echo using with underscore
Date: Fri, 14 Apr 2017 09:50:39 +0200
[Message part 1 (text/plain, inline)]
On Apr 14, 2017 08:41, "Edmond Yuen" <lordedmond <at> gmail.com> wrote:

Please verify the script below:

#/bin/bash

D=`date +%Y%m%d`
i="someparameter"

echo $D_$i
echo $D\_$i
echo $D
echo $i
echo _




//   my system is debian with coreutils 8.26-3
//
//   db=`date -d "-1day" +%Y%m%d`
//   i="0"
This is the result from my system

~$ echo "$db_$i"
0
~$ echo "$db\_$i"
20170413\_0
~$ echo "$db$i"
201704130


This output looks ok to me.
Do you expect something different? and why?
[Message part 2 (text/html, inline)]

Information forwarded to bug-coreutils <at> gnu.org:
bug#26491; Package coreutils. (Fri, 14 Apr 2017 15:51:02 GMT) Full text and rfc822 format available.

Message #14 received at 26491 <at> debbugs.gnu.org (full text, mbox):

From: Assaf Gordon <assafgordon <at> gmail.com>
To: Edmond Yuen <lordedmond <at> gmail.com>, 26491 <at> debbugs.gnu.org
Subject: Re: 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






Added tag(s) notabug. Request was from Assaf Gordon <assafgordon <at> gmail.com> to control <at> debbugs.gnu.org. (Fri, 14 Apr 2017 15:51:02 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 26491 <at> debbugs.gnu.org and Edmond Yuen <lordedmond <at> gmail.com> Request was from Assaf Gordon <assafgordon <at> gmail.com> to control <at> debbugs.gnu.org. (Fri, 14 Apr 2017 15:51:02 GMT) Full text and rfc822 format available.

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sat, 13 May 2017 11:24:05 GMT) Full text and rfc822 format available.

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.