0

I have a variable : str='abc'

I would like to copy it n times, with a space between each string, so the echo gives, for n=3 :

echo $str
abc abc abc

I don't want a space after the last abc

I've already seen the link mentionned but they doesn't help. The last answer from @Amadan is the good one thanks, but I can't accept it :/

4

1 Answer 1

0

One more way:

eval echo {1..$n} | sed "s/[[:digit:]]*/${var}/g"

Example:

n=3
var=abc

eval echo {1..$n} | sed "s/[[:digit:]]*/${var}/g"
abc abc abc
Sign up to request clarification or add additional context in comments.

3 Comments

Note: has problems when $var has regexp special characters (or slash); fine otherwise.
@Amadan, is it OK by replacing '/' with '|' symbol in sed delimiters like eval echo {1..$n} | sed "s|[[:digit:]]*|${var}|g" ?
Of course. But then your variable is restricted from having | in it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.