-4
printf "%50s\n" " I ate 4 eggs"

In this example I want to assign 50 from a variable. And How can I use the variable in for loop in bash?

Any Idea?

closed as unclear what you're asking by muru, schily, Thomas, Jeff Schaller, slm Jul 29 '18 at 4:15

Please clarify your specific problem or add additional details to highlight exactly what you need. As it's currently written, it’s hard to tell exactly what you're asking. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    Entirely unclear what you are trying to do. – Jesse_b Jul 21 '18 at 15:36
  • I am just trying fun, I do anything what comes on my mind. – barishatipoglu Jul 21 '18 at 18:45
5

At least with bash, you can pass a variable field width using the C-style printf * conversion modifier:

width=50
printf '%*s\n' $width "I ate 4 eggs"
                                      I ate 4 eggs

See man 3 printf for details.

How to use it in a for loop will depend on what your desired output is.


Based on your comment, you could use

for i in {45..50}; do 
  for j in {1..13}; do 
    printf '%*.*s\n' $i $j "I ate 4 eggs"
  done
done

to supply both the field width and precision as variables, resulting in

                                   I
                                   I
                                  I a
                                 I at
                                I ate
                               I ate
                              I ate 4
                             I ate 4
                            I ate 4 e
                           I ate 4 eg
                          I ate 4 egg
                         I ate 4 eggs
                         I ate 4 eggs
                                     I
                                    I
                                   I a
                                  I at
                                 I ate
                                I ate
                               I ate 4
                              I ate 4
                             I ate 4 e
                            I ate 4 eg
                           I ate 4 egg
                          I ate 4 eggs
                          I ate 4 eggs

and so on.

  • for i in {45..50}; do for j in {1..13}; do printf "%${i}.${j}s\n" "I ate 4 eggs"; done; done – barishatipoglu Jul 21 '18 at 15:57
  • @barishatipoglu please remember to accept the answer you found most helpful. – roaima Jul 28 '18 at 22:53
0

You can assign 50 to a variable like so:

variable=50

You can use that variable in a for loop like so:

for i in foo bar baz; do
    printf "%${variable}s\n" "I ate 4 eggs"
done

In case you are trying to increment the padding on your printf statement you could do something like this:

$ for i in {45..50}; do printf "%${i}s\n" "I ate 4 eggs"; done
                                 I ate 4 eggs
                                  I ate 4 eggs
                                   I ate 4 eggs
                                    I ate 4 eggs
                                     I ate 4 eggs
                                      I ate 4 eggs

Not the answer you're looking for? Browse other questions tagged or ask your own question.