Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
796 views
in Technique[技术] by (71.8m points)

linux - Unix pipe into ls

I thought I understood *nix pipes until now... I have an executable called studio which symlinks to my install of Android Studio and I had assumed I could get the linked-to location with

which studio | ls -l

But that doesn't work. What it gives me is equivalent to having just run ls -l in the current directory.

If I run which studio, I get /home/me/bin/studio. And if I run ls -l /home/me/bin/studio I get the expected output showing me the symlink location.

So why doesn't the piped version work? What haven't I grokked about pipes?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To do that you need xargs:

which studio | xargs ls -l

From man xargs:

xargs - build and execute command lines from standard input

To fully understand how pipes work, you can read What is a simple explanation for how pipes work in BASH?:

A Unix pipe connects the STDOUT (standard output) file descriptor of the first process to the STDIN (standard input) of the second. What happens then is that when the first process writes to its STDOUT, that output can be immediately read (from STDIN) by the second process.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

2.1m questions

2.1m answers

60 comments

57.0k users

...