Hacker Public Radio

Your ideas, projects, opinions - podcasted.

New episodes Monday through Friday.


HPR4667: UNIX Curio #9 - printf

Hosted by Vance on 2026-06-23 01:00:00
Download or Listen

HPR Comments


Whiskeyjack says: HPR4667 - printf

RE: hpr4667::2026-06-23 UNIX Curio #9 - printf by Vance
00:20:39 Listen in ogg, opus, or mp3 format.
This is a very interesting series, and I hope that you continue with it.

Here's a few other very useful things that you can do with printf that have made use of.

In a previous episode I mentioned that if you wish to concatenate several audio files into one using ffmpeg you need to create a file containing the names of the files plus some additional text.

You could write a loop writing out each line of the file one at a time.

Or you could do this with a single printf command as follows:

printf "file '%s'\n" *.flac > podseglist.txt

This will create a file consisting of multiple lines of text, each in the form of the word "file" followed by the name of the file and then a line feed. No loop is necessary.

Another example came up with something that I was working on yesterday. I needed to print a floating point time value by combining separate seconds and microsecond numbers.

However the microsecond number is not zero padded on the left and so would give an incorrect value if used it as is. This was readily solved however by using printf to zero pad the number.

padusec=$( printf '%06d' $timeusec )

I was then able to take this result and append it to a string containing the time in seconds followed by a decimal separator and then the zero padded time in microseconds.


It is things like these that make shell scripting fun to use.

candycanearter07 says: learned something new :)

RE: hpr4667::2026-06-23 UNIX Curio #9 - printf by Vance
00:20:39 Listen in ogg, opus, or mp3 format.
I had no idea that you could use %s and multiple arguments to repeat the same line multiple times! That would definitely be useful for some bash scripts

i personally moved to using ruby for my own scripts but its still really cool to know :D

xmanmonk says: Another great show!

RE: hpr4667::2026-06-23 UNIX Curio #9 - printf by Vance
00:20:39 Listen in ogg, opus, or mp3 format.
Thanks again for all your work on these episodes. Lots of good info about printf. I usually futz around with echo to get it to work on my machine, but may not work on others. There are good reasons here to use printf instead.

Mastodon Comments



More Information...


Copyright Information

Unless otherwise stated, our shows are released under a Creative Commons Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license.

The HPR Website Design is released to the Public Domain.