In a bash script, I have to include the same file several times in a row as an argument. Like this:
convert image.png image.png image.png [...] many_images.png
where image.png
should be repeated a few times.
Is there a bash shorthand for repeating a pattern?
You can do this using brace expansion:
convert image.png{,,} many_images.png
will produce:
convert image.png image.png image.png many_images.png
Brace expansion will repeat the string(s) before (and after) the braces for each comma-separated string within the braces producing a string consiting of the prefix, the comma-separated string and the suffix; and separating the generated strings by a space.
In this case the comma-separated string between the braces and the suffix are empty strings which will produce the string image.png three times.
This works with a given integer (10 in the example below).
$ echo $(yes image.png | head -n10)
image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png
It can also be used with xargs
:
$ yes image.png | head -n10 | xargs echo
image.png image.png image.png image.png image.png image.png image.png image.png image.png image.png
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With