Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8522687
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T07:11:42+00:00 2026-06-11T07:11:42+00:00

I have following function: s() { c=$1; while((c–)) ; do printf ‘x’ ; done

  • 0

I have following function:

s() { c=$1; while((c--)) ; do printf 'x' ; done }; s 2

So x is printed two times.

Is it possible to simplify this code ?

At first I’d like to skip assignation part c=$1; and do:

while((1--))

Do you have any ideas ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T07:11:43+00:00Added an answer on June 11, 2026 at 7:11 am

    Try this:

    s() { for i; do printf 'x' ; done }; s 2 3
    

    Here the for loop takes by default the arguments passed to the function and assigns them to i, so you don’t need c=$1 anymore.

    Ok, I haven’t answered your problem directly.

    To modify parameters, you must use set -- word, for example:

    > set -- 5
    > echo $1
    5
    > set -- $(( $1-1 ))
    > echo $1
    4
    

    Your function will be :

    s() { while (( $1 )); do echo 'x'; set -- $(( ${1}-1 )); done }
    

    But you will lose a little performance and clarity in the script :

    >time s 100000 > /dev/null
    real    0m1.235s
    user    0m1.196s
    sys     0m0.040s
    

    While with s() { c=$1; while (( c-- )); do echo 'x'; done }:

    >time s 100000 > /dev/null
    real    0m0.520s
    user    0m0.496s
    sys     0m0.024s
    

    I have tried this one :

    s1() { for i in $(eval echo {1..${1}}); do printf 'x'; done }
    

    But the main problem is the creation of a child process for the eval statement. The performance is a little faster than above, but still a little difficult to read:

    >time s1 100000 > /dev/null
    real    0m0.453s
    user    0m0.412s
    sys     0m0.040s
    

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following basic function: <script type=text/javascript> function Form_Data(theForm) { var t=1; while
I have the following: $(function() { $('.ajaxloader').click(function(event) { var target = $(this).attr('href'); window.location.hash =
I have the following function inside my WCF service. This function is used to
I have the following php function which i wrote a while ago. Now however,
I have following function in one Activity public void AppExit() { Editor edit =
In my MVC application in Controller i have following function to add and focus
have the following function on my collection : getFiltered: function (status, city) { return
I have the following function set up to sequentially fade in divs (with class
I have the following function to remove a DOM element div, $('#emDiv').on(click, ':button[data-emp-del=true]', function
I have the following function //simple function with parameters and variable function thirdfunction(a,b,c,d){ console.log(the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.