I was playing with bash a bit and was trying to assign a custom descriptor to a file to read from or write to.
Here is what i did:
exec 5> hello.txt # assigned 5 to hello.txt
echo "hello brother" >& 5 # works
Now the thing that i am not understanding is that if i do this instead
echo "some text" > 5 # does not work, creates a file named '5'
It does not work like the above >&.
I understand:
>&redirects stdout & stderr>redirects stdout
What is it that i am missing, is >& necessary when using descriptor?
CodePudding user response:
I understand:
This is exactly the problem - >&5 is ambiguous. That's why you shouldn't use >& and prefer > file 2>&1. See https://wiki.bash-hackers.org/scripting/obsolete
There is a special logic, that if the next token after & is a digit, then it's redirection of stdout to a file descriptor > &5. https://github.com/bminor/bash/blob/ec8113b9861375e4e17b3307372569d429dec814/parse.y#L645
