Je Sat, Jan 25, 2025 at 03:56:42PM +0100, Kusalananda Kähäri skribis:
> Je Sat, Jan 25, 2025 at 07:16:14AM +0000, Ross skribis:
> > 
> > Configuration Information [Automatically generated, do not change]:
> > Machine: x86_64
> > OS: linux-gnu
> > Compiler: gcc
> > Compilation CFLAGS: -g -O2 -fno-omit-frame-pointer -mno-omit-leaf-
> > frame-pointer -flto=auto -ffat-lto-objects -fstack-protector-strong -
> > fstack-clash-protection -Wformat -Werror=format-security -fcf-
> > protection -Wall
> > uname output: Linux X220-Bravo 6.8.0-51-generic #52-Ubuntu SMP
> > PREEMPT_DYNAMIC Thu Dec  5 13:09:44 UTC 2024 x86_64 x86_64 x86_64
> > GNU/Linux
> > Machine Type: x86_64-pc-linux-gnu
> > 
> > Bash Version: 5.2
> > Patch Level: 21
> > Release Status: release
> > 
> > Description:
> > >From the command line
> >     printf "%*s\n" 80 " " | tr " " "*"
> > does just what I'd expect generating a banner line of asterisks.
> > 
> > However:
> >     foo=$(printf "%*s\n" 80 " " | tr " " "*")
> >     echo $foo
> > acts very differently; it seems to perform ls or something similar.
> > 
> > The same occurs with the more standard printf "%80s" form. 
> > 
> > Repeat-By:
> > As above.
> > 
> > Fix:
> > Unknown.
> > 
> 
> This is no bug in bash but in your shell code.  There is almost never a
> reason *not* to double quote a variable substitution.  Doing so will
> prevent the shell from splitting the value of the variable on the
> characters in $IFS (space, tab, and newline, by default), but also from
> using the split-up value as filename globbing patterns, which is what
> happens in your case.
> 
> Just double quote the variable expansion:
> 
>       echo "$foo"
> 
> 

Sorry, I pressed send too quickly. I was going to suggest an alternative
to using a command substitution and tr:

        printf -v foo '%*s' 80 ' '
        echo "${foo// /*}"

-- 
Andreas (Kusalananda) Kähäri
Uppsala, Sweden

.

Reply via email to