Unix pipe into ls

LinuxUnixPipeLs

Linux Problem Overview


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?

Linux Solutions


Solution 1 - Linux

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.

Solution 2 - Linux

ls does not read its arguments from standard input, but from the command line. To get the directory at the command line, you have to use command substitution:

ls -l "$( which studio )"

(The double quotes are needed if the path might contain whitespace.)

Solution 3 - Linux

Since ls -l does not take any input, it does not do anything regarding the output of which studio. The important thing here is understanding the difference between standard input and arguments. The standard input is a special file that is read using the scanf procedure (by a program in C for example), and the arguments to a program are passed to the main procedure as the argv and argc parameters. argv is an array of null terminated arrays of char, and argc is the length of that array.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionhcarverView Question on Stackoverflow
Solution 1 - LinuxfedorquiView Answer on Stackoverflow
Solution 2 - LinuxchorobaView Answer on Stackoverflow
Solution 3 - LinuxlamgView Answer on Stackoverflow