LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Software (https://www.linuxquestions.org/questions/linux-software-2/)
-   -   [Ubuntu] Process substitution prints out file descriptor? (https://www.linuxquestions.org/questions/linux-software-2/%5Bubuntu%5D-process-substitution-prints-out-file-descriptor-4175604922/)

Weapon S 04-29-2017 01:56 PM

[Ubuntu] Process substitution prints out file descriptor?
 
Every time I use process substitution, it prints out the file descriptor of the temporary pipe afterwards. Simplest example (I wouldn't expect any output):
Code:

$ echo <( false )
> /dev/fd/63

I don't remember I've ever had this issue with any other Linux while using process substitution. Is there any option, command, or syntax I can use to suppress this output? Or did I just remember incorrectly?

!!! 04-29-2017 05:31 PM

Try cat instead of echo. Do you literally=actually get that > character displayed on the output=result line? (I don't)
Now that I've knocked this thread off of pristene ZRT status, hopefully LQgurus will come along with a detailed explanation;)
A web-search of "/dev/fd/63" returns somethings I remember searching for in the past, like: https://unix.stackexchange.com/quest...hich-is-a-pipe

Edit: Found MUCH BETTER! link: http://tldp.org/LDP/abs/html/process-sub.html
And, adding ThreadTitle, http://wiki.bash-hackers.org/syntax/...ion/proc_subst
Wiki has echo<(true) http://wikipedia.org/wiki/Process_substitution

But I don't have a precise&full explanation. (Interesting study tho!)
p.s. Minor 'nit': suggest adding "bash" to ThreadTitle (so viewers don't think 'krnl' e.g. ;) )

rknichols 04-29-2017 07:21 PM

Quote:

Originally Posted by Weapon S (Post 5704015)
Code:

$ echo <( false )
> /dev/fd/63


Did you think you had redirected the output from false to the stdin of the echo command? That is not what you did. That form of process redirection sets up a file descriptor and passes an argument that is a pathname that can be opened to access that file descriptor. The echo command does not open files. It just echoes its arguments.

If you want to redirect a command's stdin as with a pipe, you can do it this way:
Code:

command1 < <( command2 )
Note the space between the two "<" characters.

I'm sure some will wonder why on Earth you would do that rather than simply writing "command2 | command1", but that construct is useful for streams other than stdin and for redirecting the stdin of loops in shell scripts:
Code:

X=0
while read Var; do
    $((X += Var))
done < <(command)
echo "Sum=$X"

If you tried to do that with "command | while read Var; do ... done", the while loop would be run in a sub-shell, and the incremented value of X would not be available outside the loop.

Weapon S 04-30-2017 12:12 AM

Yep, I'm stupid. The actual program I had trouble with was wc (to be exact: "wc -l <( find /home/me -size -2k )" ). Then to make a minimum replication of the problem, I used echo. Echo will just print out the argument it gets, and wc will print out the file(s) it counted in at the end.


All times are GMT -5. The time now is 10:13 AM.