ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
While I consider them too long & complex to be considered elegant, your nested loops can be easily fixed:
Code:
for (( a=1; a <= 9; a++ ))
do
for (( b=1; b <= a; b++ ))
do
echo -n "$b"
done
echo
done
Timing
Just for fun, I checked the time used by the various techniques suggested using time. To get some to register a measurable time, I had to push n/a/i to 99.
The results surprised me: The hands down winner was:
Code:
for ((i=1;i<=99;i++))
do
j=${j}${i}
echo ${j}
done
Thank you very much dear "archtoad6".......I was looking for this answer...Thank you once again for everyone who helped me...I got the output....YOU GUYS ROCK!!!!!!
I took the three scripts from archtoad6's post and run the same tests, but got different results. After several running and looking at the times, the three scripts were very close to each other (in my --slow-- machine, about 0.050 s).
Then, only for curiosity, I took one the scripts and changed it a "little":
Code:
for ((i=1;i<=99;i++))
do
j=${j}${i}
echo "echo \"${j}\""
done
to create a new script with goes like
Code:
echo "1"
echo "12"
echo "123"
...
Which turned to be slower (first 2 runs), but after the third one it was slightly faster ( about 0.040s), some caching probably helping...
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.