Re: problems with loop in bash

houghi wrote:

Janis Papanagnou wrote:
I am not sure you understood the article. But, anyway, again; consider
that you're not only writing code for yourself but also come here often
to let the audience fix your problems.

I probably didn't understand it, which is logical as I am not as smart
as most others. ;-)

Just for your information, I tried using print (well, printf as print
does not exist on my machine apparently) and I had after an hour still
no luck to get anything close to what I would have gotten with 'echo'.
(and no, I will nore bnore people here with it.)

So it is not an unwillingness to learn, nor is it being stubborn. It is
accepting the limitations of what I can do. Again, if this results in
people killfiling me or just not responding, I understand and respect

If people are willing to help me, great. If not, also great. I will try
for several more hours to try to get the hang of printf and just hope
that is not time wasted.

At the very simplest, to emulate

echo "$variable"

with printf, you do

printf "%s\n" "$variable"

This is just 1% of what printf can do. The general format of printf is

printf <format string> <arguments>

"Format string" is a normal string, but it contains some special sequences
that are special to printf, called "format specifications". "%s" above is
one of those. When printf sees one of those sequences, it replaces it with
the next argument in its list, and formats it accordingly. It follows that
(usually) if the format string contains n format specifications, you have to
supply n arguments to printf after the format string. For example, the
format specification %d is to print integers, so you do this:

printf "The sum of %d and %d is %d.\n" "$num1" "$num2" "$((num1+num2))"

If $num1 is 4, and $num2 is 5, the above prints

The sum of 4 and 5 is 9.

The format specifications are many, %s and %d are just two of them.
Simplifying, %s is for strings, %d for integers, %f and %g for floating
point numbers, %c for characters, %% for a literal %. There are many others,
and also each specification can have optional modifiers that change the
formatting. For example, %5d is like %d, but uses five characters to print
the integer, even if it's shorter (pads with spaces); likewise, %05d pads
with zeros.

Printf has another advantage over echo: it recognizes many special escape
sequences that echo does not recognize (or recognizes only with -e in
certain implementations), like \n or \t.

Again this just scratched the surface, but hopefully now you should get the
idea and the rest is just reading the man page to learn all about the format
specifications, the optional modifiers and the escape sequences. In case you
shell's man page for printf if scant, I suggest you do "man 3 printf" to see
the C printf man page with the full specs, to which the shell's printf is
mostly compliant (with the obvious differences/limitations).

Relevant Pages

  • Re: ES format
    ... >> runtime format, even a runtime format that is trivially constant, not. ... You aren't allowed by Standard C to define any _external_ linkage ... In particular you certainly can define a 'static' function printf(), ... but otherwise it is part of the language and the compiler ...
  • Re: PRI* considered harmful
    ... Now we're safe for every possible underlying type of any typedef name, ... the problem because of the different format specifications between double ... The _main_ argument about the insanity of printf(), ... standard functions, any better. ...
  • Re: vscanf and vprintf
    ... They take a pointer to a list of arguments rather than the arguments ... How can I write a function that takes a format string and a variable ... number of arguments, like printf, and passes them to printf to do most ... variable-length argument list, they accept a single va_list pointer. ...
  • Re: how does printf handle an unknown number of arguments
    ... When you include stdio.h, you include a prototype for printfthat, ... int printf; ... The method printf() uses is a format string, which has embedded within it, ...
  • Re: printf troubles
    ... is not part of the awk language. ... If I read your response correctly, printf cannot accept a variable ... You can have an arbitrary number of arguments (matching to the format ...