0

I came across the following set of shell commands for reading and writing to serial ports, from this thread:

stty -speed 19200 < /dev/ttyS0 # sets the speed of the port
exec 99<>/dev/ttyS0 (or /dev/ttyUSB0...etc)
printf "AT\r" >&99
read answer <&99  # this reads just a CR
read answer <&99  # this reads the answer OK
exec 99>&-

I am having trouble understanding the lines that use file descriptors, particularly these two lines:

exec 99<>/dev/ttyS0 (or /dev/ttyUSB0...etc)

and

exec 99>&-

What are they doing? Is there any reason why 99 is being used as opposed to any other number? Any help is appreciated. Thanks!

  • 2
    There is no special meaning of 99; that was just "a file descriptor that we are pretty sure won't be in use". – larsks Jul 21 '22 at 17:51

1 Answers1

1

As mentioned in a comment this is just identification of this file handler. Just like STDIN have ID 0, STDOUT have ID 1, STDERR have ID 2.

For example:

echo aa >/dev/null

and

echo aa 1>/dev/null

are the same

Romeo Ninov
  • 17,484
  • Thanks. Is there any reason we are opening a file descriptor instead of directly redirecting into the file by its name? – First User Jul 21 '22 at 18:53
  • 1
    @FirstUser, this will create more unified way to redirect different messages. Also you will have only one place to change the file name (in case of need) – Romeo Ninov Jul 21 '22 at 19:03