Question

I'm trying to print the following pattern using printf and seq:

0000
0001
0002
0003

My problem is once I use:

seq 0 10 | xargs printf %04d

all my output is formatted into the same line likeso:

0000000100020003

I still can't get the hang of using xargs. How do I use it correctly in this case?

Was it helpful?

Solution

The printf command does not output a line break if you don't ask it to. Try:

seq 0 10 | xargs printf '%04d\n'

Note that you can achieve the same with just seq, since it allows specifying a printf-style format:

seq -f %04g 0 10

OTHER TIPS

you don't need printf or xargs. seq has -f option:

kent$  seq -f '%04G' 10
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
seq 0 10 | xargs printf "%04d\n"

The original question is missing the newline character at the end of the printf. Simply adding a newline character fixes the issue.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top