GNU bug report logs - #24054
tr bug? [:space:] acts like "a"

Previous Next

Package: coreutils;

Reported by: <pmun <at> tutanota.de>

Date: Fri, 22 Jul 2016 20:20:01 UTC

Severity: normal

Tags: notabug

Done: Pádraig Brady <P <at> draigBrady.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: pmun <at> tutanota.de, 24054 <at> debbugs.gnu.org
Subject: bug#24054: tr bug? [:space:] acts like "a"
Date: Fri, 22 Jul 2016 17:18:27 -0400
tag 24054 notabug
close 24045
stop

Hello,

On 07/22/2016 02:58 PM, pmun <at> tutanota.de wrote:
> For the last few weeks tr [:space:] isn't working the way it used to for me under Ubuntu 14.04. Instead of acting on spaces it acts on "a"s.:
>
> me <at> U:~$ echo "thisstring abcde what the heck?" | tr [:space:] '_'
> thisstring _bcde wh_t the heck?
>
> This is 64 bit 14.04 built up from the mini.iso with just plain Openbox as the only DE, in lxterminal using bash.

This is not a bug, but a result of your shell (bash) performing filename completion on single-letter filenames in your current directory.

In bash, the syntax [X] without quotes does filename completion, just like "*" or "?".

I would guess that in the last few weeks you created a file called 'a' in the directory,
and so '[:space:]' matched it  (technically it means: match filenames of one character, one of :,s,p,a,c or e).

It used to "just work", because if the shell does not find matching files, it passes the parameter as-is to the program.

The following will demonstrate:

    $ mkdir empty
    $ cd empty

    $ echo [:space:]
    [:space:]

    $ touch a

    $ echo [:space:]
    a

    $ touch ':'

    $ echo [:space:]
    : a

    $ echo '[:space:]'
    [:space:]

Since the shell replaced '[:space:]' with 'a', the 'tr' command became:

    tr a '_'

which is the behavior you encountered.

The solution is to always quote such parameters:

    $ echo "thisstring abcde what the heck?" | tr '[:space:]' '_'
    thisstring_abcde_what_the_heck?_

(The last underscore is the newline, which also counts as whitespace).

To learn more about bash's filename expansion capabilities, see here:
  https://www.gnu.org/software/bash/manual/bashref.html#Filename-Expansion


As such I'm closing this bug report, but discussion can continue by replying to this thread.

regards,
 - assaf





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

Previous Next


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