3

In Python strings can be "multiplied" to repeat them a number of times:

test="----"
print(test)

test="----"*10
print(test)

#output
----
----------------------------------------

Is there an equivalent in bash? I tried * but it doesn't work:

$ Test2="----"
$ echo $Test2
----
$ echo ${Test2}*5
----*5
$ echo $Test2*5
----*5
$ echo echo $[Test2]*5
-bash: ----: syntax error: operand expected (error token is "-")
$ echo $(Test2)*5
Test2: command not found
2
  • 2
    printf -- "${Test2}%.s" {1..5} Commented Apr 20, 2022 at 20:29
  • That is dangerous if $Test2 might contain %, \, or other metacharacters printf will interpret. Commented Apr 20, 2022 at 20:33

1 Answer 1

3

There's no equivalent shorthand, no. You can do it with an explicit loop like so:

test=""
for ((i=0; i<5; i++)); do test+="----"; done
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.