baiumbg
baiumbg

Reputation: 23

Repeating a character using printf and outputting to variable

I'm having trouble making a string out of repeated single characters. This works when I'm not assigning it to a variable:

printf "*%.0s" $(seq 1 $(expr $1 / 2))

This just assigns the script name to test:

printf -v test "*%.0s" $(seq 1 $(expr $1 / 2))

I also tried:

test=$(printf "*%.0s" $(seq 1 $(expr $1 / 2)))

But it does the same thing.

Why doesn't this work, and is there another way to build a string and assign it to a variable?

Upvotes: 1

Views: 227

Answers (1)

chepner
chepner

Reputation: 531958

The value of test is fine; you just need to quote its expansion:

echo "$test"

Otherwise, the asterisks in the value are expanded to the contents of the current directory via pathname expansion.

Upvotes: 1

Related Questions