Redirecting Multiple stdins?

2019-05-13 21:30发布

I have three files named One, Two, Three.

One contains:

1
3
2

Two contains:

4
6
5

Three contains:

7
9
8

When I give the following command:

$sort < One < Two < Three

I get the output:

7
8
9

But when I give the following command:

$sort One Two Three

I get the ouput:

1
2
3
4
5
6
7
8
9

Can anyone please shed light on what exaclty is happening here? Why does the input from 1 and 2 not taken into consideration in the first command?

2条回答
唯我独甜
2楼-- · 2019-05-13 21:57

You can't redirect multiple files with bash. To work around this limitation you could use cat:

cat 1 2 3 | sort

On a side note, zsh supports what it calls mutlios:

 zsh$ setopt multios
 zsh$ sort < 1 < 2 < 3 > 4 > 5
 zsh$ tr '\n' ' ' < 4 < 5
 1 2 3 4 5 6 7 8 9 1 2 3 4 5 6 7 8 9 
查看更多
冷血范
3楼-- · 2019-05-13 22:04

Your command is the same as:

sort 0<1 0<2 0<3

(file descriptor 0 is standard input)

Redirections are processed in the order they appear, from left to right.
sort command itself cannot see any of those files.
bash open file 1,2,3 at file descriptor 0 one by one.
So the right most one override left ones.
At last, sort read from file descriptor 0 which is bind to file 3.

查看更多
登录 后发表回答