Question

Title sums it up.

$ echo `seq 0 10` `seq 5 15` | sort -n
0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15

Why doesn't this work?

Even if I don't use seq:

echo '0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15' | sort -n
0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15

And even ditching echo directly:

$ echo '0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15' > numbers
$ sort -n numbers 
0 1 2 3 4 5 6 7 8 9 10 5 6 7 8 9 10 11 12 13 14 15
Was it helpful?

Solution

sort(1) sorts lines. You have to parse whitespace delimited data yourself:

echo `seq 0 10` `seq 5 15` | tr " " "\n" | sort -n

OTHER TIPS

Because you need newlines for sort:

$ echo `seq 0 10` `seq 5 15` | tr " " "\\n" | sort -n | tr "\\n" " "; echo ""
0 1 2 3 4 5 5 6 6 7 7 8 8 9 9 10 10 11 12 13 14 15
$

You have single line of input. There is nothing to sort.

The command as you typed it results in the sequence of numbers being all passed to sort in one line. That's not what you want. Just pass the output of seq directly to sort:

(seq 0 10; seq 5 15) | sort -n

By the way, as you just found out, the construct

echo `command`

doesn't usually do what you expect and is redundant for what you actually expect: It tells the shell to capture the output of command and pass it to echo, which produces it as output again. Just let the output of the command go through directly (unless you really mean to have it processed by echo, maybe to expand escape sequences, or to collapse everything to one line).

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