Redirecting stdin and stdout to the same device in shell

25 views Asked by At

I'm looking for a way to redirect stdin and stdout to the same character device. I know I can do it manually:

rz </dev/ttyUSB0 >/dev/ttyUSB0

However, it feels repetitive. On top of that, shellcheck complains:

Make sure not to read and write the same file in the same pipeline

It's quite easy to redirect stdout and stderr to the same file (cmd >file 2>&1) but it won't work for stdin and stdout:

$ rz >/dev/ttyUSB0 <&1
rz waiting to receive.Retry 0: Got TIMEOUT
Retry 0: Got TIMEOUT
1

There are 1 answers

2
proski On

I was able to come with a solution that works in bash and dash but not in fish:

rz <>/dev/ttyUSB0 >&0

<> opens file in read-write mode. It is used as stdin. >&0 redirects stdout to the file descriptor used by stdin.