[linux] printf Was: Wat is er fout in dit bash-script?

Geert Stappers stappers op stappers.it
Vr Sep 30 10:33:33 CEST 2022


On Fri, Sep 30, 2022 at 09:35:39AM +0200, Paul Slootman via Linux wrote:
> On Thu 29 Sep 2022, Eric de Hont via Linux wrote:
> 
> > Voor het exacte formaat van de formatstring van printf:
> > 
> >    man 3 printf
> 
> De "3" hier geeft aan dat je de info uit section 3 van de manpages wilt;
> dat gaat over (C) library functions. Section 1 is voor gewone
> commando's.
> 
> "help printf" geeft (in bash) de informatie over de in bash ingebouwde
> printf functie (die weer verwijst naar man 1 printf ...)
> 

Wat experimenten met  printf

$ printf hoi
hoi$ printf hoi\n
hoin$ printf "hoi\n"
hoi
$ printf "hoi\n"
hoi
$ printf "hoi %s\n"
hoi 
$ printf "hoi %s\n" bob
hoi bob
$ printf "hoi %s.\n" bob
hoi bob.
$ printf "hoi %s.\n"
hoi .
$ printf "hoi %s.\n" bob
hoi bob.
$ printf "Getal %s.\n" bob  # verwacht string als invoer
Getal bob.
$ printf "Getal %s.\n" 3  # gaat ook goed
Getal 3.
$ printf "Getal %d.\n" 33  # verwacht cijfers
Getal 33.
$ printf "Getal %d.\n" tekst  # echter cijfers worden verwacht
bash: printf: tekst: ongeldig getal
Getal 0.
$ printf "Getal %04d.\n" 33  # altijd vier cijfers, aanvullen met 0
Getal 0033.
$ printf "Getal %04d.\n" 4321  # altijd vier cijfers, aanvullen met 0
Getal 4321.
$ printf "Getal %10d.\n" 4321  # 'Getal ' + 10 posities
Getal       4321.
$ printf "Getal %10d.\n" 987654321  # 'Getal ' + 10 posities
Getal  987654321.
$ printf "Getal %10d.\n" 9876054321  # 'Getal ' + 10 posities
Getal 9876054321.
$ 


Doe er je voordeel mee


Groeten
Geert Stappers
-- 
Silence is hard to parse


Meer informatie over de Linux maillijst