A repeating string that supports variables under the shell

Source: Internet
Author: User

Python is a very easy way to solve repetitive strings, such as a= "James", so to get "Jamesjamesjames" is "a*3".


But this "*" string multiplication in the shell is not, for example, "to output 10 a", do not want to use for the loop to do, how to do?


First of all know that if just want to output 1~10 These 10 numbers, is very simple:

#echo {1..10} will get: 1 2 3 4 5 6 7 8 9 10

And the use of #seq 1 can also, but this is vertical, if you want to be horizontal, it is #seq- S "" 1,-S is the meaning of the specified delimiter.

But if divert,#echo "a" {1..10} , will the output be 10 a?

Unfortunately, the output is A1 A2 A3 A4 A5 A6 A7 A8 A9. It's very annoying to have a gap with the expected target.

But this method still has his role, such as #echo {a,b,c}{1,2,3}

The above command produces the following results: A1 A2 A3 B1 B2 B3 C1 c2 C3.


#echo a {,,,,,,,,,,}, the parentheses are 10 commas, so what's the effect?

The effect is a a a a a a a a a a A.


Seems very close, but this method has a space do not say, and to manually hit 10 comma, there is this time I simply hit 10 a more good, and if the output of 1000 A, I would like to hit 1000 comma?


So here's a hard way to do it:

str=$ (printf "%-10s" "a") echo "${str///A}"

So the output is 10 a. But what does this parameter mean? Here I make a slight change:

str=$ (printf "%-5s" "O") echo "${str///x0}"

The output at this time is ox0x0x0x0, and it is visible that the number following the STR represents the repetition, and the next word is the beginning. The following echo output "X0" is a specific fill item. The str//in curly braces is fixed and cannot be added in the middle.


This method is good, but it is too troublesome, not remember.


In a different way, how about using #printf%.sa {1..10} ?

The output is aaaaaaaaaa, no spaces, but if you want to support variables, try again.

650) this.width=650; "src=" Http://s5.51cto.com/wyfs02/M02/80/65/wKiom1c_yZ_CIibGAABIuOApugA002.png "title=" 1.png " alt= "Wkiom1c_yz_ciibgaabiuoapuga002.png"/>


Variables are not supported.


How can I support a variable? Still looking.

This article is from "Life is waiting for Gordo" blog, please make sure to keep this source http://chenx1242.blog.51cto.com/10430133/1775649

A repeating string that supports variables under the shell

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.