How to pipe a list of numbers straight from the shell into a command

bashpipeshellsyntax

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.)

Best Answer

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.

Related Question