1

There is a space in between

$ echo {0..9}
0 1 2 3 4 5 6 7 8 9

How do I produce similar output using echo {0..9} without space?

Desired output

0123456789
αғsнιη
  • 41,407
Wolf
  • 1,631
  • 1
    echo 0123456789 –  Oct 08 '20 at 06:17
  • 1
    0-9 is just an example, what if you've up to 1000? Still want to do it manually? – Wolf Oct 08 '20 at 06:19
  • Why not? It's only 2895 bytes. But what would a line containing numbers not separated by spaces (012...9989991000) be good for? –  Oct 08 '20 at 06:30

5 Answers5

10

seq allows you to specify a separator using -s:

$ seq -s '' 0 9
012345678
10

try:

echo -e \\b{0..9}

from man echo:

-e     enable interpretation of backslash escapes
\b     backspace

or better to use printf:

printf '%s' {0..9}

or to add last newline:

printf '%s' {0..9} $'\n'
αғsнιη
  • 41,407
  • It is worth mentioning that the backspace is a byte on its own right and therefore should be avoided if this is going to be redirected to a file, for example. The Printf solution is cleaner in that regard. – Quasímodo Oct 08 '20 at 11:49
  • @Quasímodo absolutely, yes. – αғsнιη Oct 09 '20 at 06:49
5

Use tr to remove the spaces:

$ echo {0..9} | tr -d ' '

tr man page:

       -d, --delete
              delete characters in SET1, do not translate
z.h.
  • 994
3

For the record, note that {0..9} does come from zsh initially.

Like csh's {a,b} which it builds upon, it expands to separate arguments.

So for those to be bundled back together on output, you need a command that prints or can print its arguments joined with each other, like printf:

printf %s {0..9}

(though that doesn't print the terminating newline, see αғsнιη's answer to work around it)

Or have an extra step that does the joining.

In zsh, you can join elements of an array with nothing in between with ${(j[])array}, so you could use an anonymous function like:

() { print ${(j[])@}; } {0..9}

or

function { print ${(j[])@}; } {0..9}

In Korn-like shells (including bash and zsh), to join elements of any array with nothing, you use "${array[*]}" whilst $IFS is empty:

set {0..9}
IFS=
echo "$*"

(here using echo instead of print, as though bash is a Korn-like shell even more so than zsh, it didn't implement the print builtin).

With bash or zsh, you can also do the joining into a variable with:

printf -v var %s {0..9}
0
echo $'\e[D'{0..9}
0123456789

In the terminal it looks solid but is actually connected by the separator out of the terminal control sequences.

echo $'\e[D'{0..9} | cat -vet
^[[D0 ^[[D1 ^[[D2 ^[[D3 ^[[D4 ^[[D5 ^[[D6 ^[[D7 ^[[D8 ^[[D9$

'\e[D' - move cursor left
echo $'string' or echo -e 'string' - enable interpretation of backslash escapes.

nezabudka
  • 2,428
  • 6
  • 15