How to avoid printing a newline when seq completes

.seqgnu

To create a column header, looking like:

1234567890123456789

I (am trying to) use seq and echo:

seq -s '' 1 9 ; echo -n 0; seq -s '' 1 9

However, seq outputs a newline after each run. How can I avoid that?

Best Answer

Assuming you just want to print 1234567890123456789, you can do it with:

$ printf "%s" $(seq 1 9) $(seq 0 9)
1234567890123456789$

That won't have a trailing newline at all though, so maybe you prefer:

$ printf "%s" $(seq 1 9) $(seq 0 9) $'\n'
1234567890123456789
$

A few simpler choices if you don't need to use seq:

$ perl -le 'print 1..9,0,1..9'
1234567890123456789
$ printf "%s" {1..9} {0..9} $'\n'
1234567890123456789

Since you mentioned portability, I recommend you use the perl approach, or if you are likely to encounter systems without perl, and yet need the same command to run in shells including bash, sh, dash, tcsh etc, try Kamil's approach.

Related Question