How do I pipe a list of numbers straight from the shell into a command?

How do I pipe a list of numbers straight from the shell into a command? For exampe something like this

[1,2,3,4] | sort


would give

1
2
3
4


EDIT:

In response to the answers kindly posted so far . . . I ask this, because I want to quickly test and debug a console application that takes many numbers as it input without having to type lots of individual values followed by carriage returns. I'd like to just type in the 'one liner' and hit the up arrow now and then to replay the command. Ideally, I'd like to do this without using a text file containing the values (which would obviously be the most simple way to do this.)

I'm assuming we're using single digit numbers - in which case echo 1 2 4 3 | grep -o [1234567890]|sort should to the trick. I think you'd have to adjust the regex for grep if its a multi digit number.

grep -o selects as per regex and prints it one per line

edit: and an even more elegant solution.

we still use echo, but with tr. This works with numbers bigger than one digit

echo 10,2,4,3|tr ',' '\n'|sort -g

tr is being told to replace a comma with a newline, and sort -g sorts in numerical order (assuming thats what you want sort for).

Assuming you need the square brackets in the list for some odd reason, you can remove it with

echo [10,2,4,3]|tr '[:punct:]' ' '|tr ' ' '\n'|sort -g

The additional tr command replaces any punctuation with a space.

• Your last example doesn't work. The first tr '[:punct:]' ' ' turns any commas into spaces before the the second tr ',' '\n' would ever see them. – Nicole Hamilton Nov 23 '12 at 8:19
• fixed I think - almost, now I have two spaces in front ... The square brackets so seem to make it a little tricky – Journeyman Geek Nov 23 '12 at 8:26
• Try tr -d '[]' | tr ',' '\n'. – Nicole Hamilton Nov 23 '12 at 8:42

You could use a "here" document using the << in-line i/o redirection operator. The shell looks for whatever token you give (in this example, eof) as the end-of-file marker for the here doc.

sort << eof
1
2
3
4
eof


Another way would be to iterate over the values, echoing them into sort:

for i in 1 2 3 4; do echo $i; done | sort  If you'd like the for loop packaged up as a command, use a procedure: list() for i in$*; do echo $i; done list 1 2 3 4 | sort  $ printf '%d\n' 1 3 2 4 | sort
1
2
3
4


or

$echo$'1\n2\n4\n3' | sort
1
2
3
4

• In some states, the second answer works better without the $. I have gotten echo '1\n2\n4\n3' to give the output you show for echo$'1\n2\n4\n3', while echo $'1\n2\n4\n3' itself outputs $1, 2, 4, and 3 (on separate lines). – Scott Jan 3 '13 at 23:02

You can use cat

cat | sort
1
2
4
3
^d (end of input)

• Except for being slower, this is no different than sort all by itself. – Nicole Hamilton Nov 22 '12 at 19:36
• @Nicole: The OP explains in an edit that sort is a metaphor for some other, unspecified program –– and that program could be testing its standard input to see whether it is a tty, file, pipe, or whatever. – Scott Jan 3 '13 at 23:00
• @Scott And how is that different than sort as a metaphor for something else, but still all by itself. The cat | adds nothing. – Nicole Hamilton Jan 3 '13 at 23:50
• @Nicole: My point is that, if you type umptyfratz and then type some input (terminated by Ctrl+D), and then type cat | umptyfratz followed by the same input, you might get two different behaviors. You said, “this is no different …” – Scott Jan 3 '13 at 23:59
• @Scott, I released before bash and, no, my users are fine. Pipes are much faster but I realized after my comment that I was able to use them cheaply because I'm multithreaded; bash is not and would have to fork to feed the pipe. But let's move this offline. Send me mail at hamiltonlabs.com. If you run Windows, I'll give you a free copy of my work and we can talk about what I've done. – Nicole Hamilton Jan 7 '13 at 16:35

The “super simple” answer: type the command, and then type the input.  (Strictly speaking, this is not piping.  The word “pipe” is vastly overused in reference to I/O redirection, and this wouldn’t even be I/O redirection, it’s just standard I/O.)  End with Ctrl+D (assuming you’re on some form of *nix or something Unix-like, and that you haven’t changed your EOF character).  For example:

(prompt)> sort –f The quick brown fox
Ctrl+D brown fox quick The
(prompt)>

The Ctrl+D may echo as ^D, and first line of the output may appear on the same line as this:

^Dbrown
fox
quick
The