I am trying to understand named pipes in the context of this particular example.
I type <(ls -l) in my terminal and get the output as, bash: /dev/fd/63: Permission denied.
If I type cat <(ls -l), I could see the directory contents. If I replace the cat with echo, I think I get the terminal name (or is it?).
echo <(ls -l) gives the output as /dev/fd/63.
Also, this example output is unclear to me.
ls -l <(echo "Whatever") lr-x------ 1 root root 64 Sep 17 13:18 /dev/fd/63 -> pipe:[48078752]
However, if I give,ls -l <() it lists me the directory contents.
What is happening in case of the named pipe?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
When you do <(some_command), your shell executes the command inside the parentheses and replaces the whole thing with a file descriptor, that is connected to the command’s stdout. So /dev/fd/63 is a pipe containing the output of your ls call.
When you do <(ls -l) you get a Permission denied error, because the whole line is replaced with the pipe, effectively trying to call /dev/fd/63 as a command, which is not executable.
In your second example, cat <(ls -l) becomes cat /dev/fd/63. As cat reads from the files given as parameters you get the content. echo on the other hand just outputs its parameters “as-is”.
The last case you have, <() is simply replaced by nothing, as there is no command. But this is not consistent between shells, in zsh you still get a pipe (although empty).
Summary:
<(command) lets you use the ouput of a command, where you would normally need a file.
Edit: as Gilles points out, this is not a named pipe, but an anonymous pipe. The main difference is, that it only exists, as long as the process is running, while a named pipe (created e.g. with mkfifo) will stay without processes attached to it.
Method 2
You misunderstand both the ls command and redirection. ls lists the files and directories given on the command line, I don’t believe it accepts any input from stdin. Redirection > >> and < are ways to use a file to give input and collect output.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0