Subject: Re: bug#24054: tr bug? [:space:] acts like "a"

From: Assaf Gordon <[email-masked]>
Date: Fri, 22 Jul 2016 17:18:27 -0400

tag 24054 notabug
close 24045
stop

Hello,

On 07/22/2016 02:58 PM, [email-masked] 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@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