So to read and write to a socket:

Write to a socket:

echo "hello" > /dev/tcp/

Read from socket:

netcat -p 27002 -l

BUT... I would like to read a socket using "cat" and not have to use something else like netcat: ?

cat /dev/tcp/


cat: /dev/tcp/ No such file or directory

Tried and hangs:

cat < /dev/tcp/

How to use "cat" to read from a socket or port?

  • 1
    That /dev/tcp/host/port paths are a feature of your bash shell, they're not real files provided by your OS. You can only use them with redirections, so cat </dev/tcp/ – user414777 Feb 12 at 23:35
  • 1
    The aptly-named socat comes to mind. Before I think about preparing an answer, could you help me verify correctness by adding some things to the question? I'm after: what is the underlying motivation? How are you creating the socket (assuming it exists before you try echoing to it)? Can we assume that the expected output is "hello"? – cryptarch Feb 12 at 23:35
  • cat < /dev/tcp/ __ bash: connect: Connection refused __ bash: /dev/tcp/ Connection refused – jdl Feb 12 at 23:37
  • 3
    netcat -l -p port does not read from that socket, but listens on that port, accepts a connection, and then either writes or reads from that connection. If you run echo yes | netcat -l -p 27003 in a window, then cat </dev/tcp/localhost/27003 in another will print yes. If you're wondering if you can do what netcat does using just bash redirections, the answer is no, you can't. – user414777 Feb 12 at 23:51
  • 1
    Nitpicking: you can use cat with input redirected from the socket, once the connection is established as a client. The socket is bidirectional. What can't be done with bash is to wait for an incoming connection. In theory waiting like this would work with UDP from a single remote peer (which will have to bind the source port), except the "listening" port will be chosen randomly by the system, because it wasn't bound first, so the remote peer needs side channel information to transmit data to the right port. – A.B Feb 13 at 10:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.