On Sat, Jan 25, 2025 at 4:06 PM Andreas Kähäri <andreas.kah...@abc.se>
wrote:

>
> 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
>
>
While we are showing printf tricks, we can notice we are cheating a bit
here, the foo variable is set to a serie of space ' ' and then each access
to this variable require a space substitute with your desired char-pattern
here '*', unless you re-assign the variable with the subsitution i.e


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

In order to get foo set up with a desired number of '*' (or any 1 char
pattern you like) you may consider this one :-)

$ printf -v foo "%(*)T" {1..4} ; echo "'$foo'"
'****'

I admit it is a kind of abuse :-)

Note the upper bound is hard coded though (as 80 in your example).

To get a parametrisable upper bound and without fork/exec (eliminating
seq(1)) we can consider this one that is still fast and more general
$ c=10
$ foo='' ; while((${#foo}<c)) do foo+='*'; done
$ echo $"'$foo'"
'**********'

I guess there are more :-)

Reply via email to