4 ms·
Bash has a /dev/tcp/$host/$port pseudo device which covers almost all use cases of Netcat just in case your machine has no Netcat installed. I still don't unde
by sprash 5y ago
Bash has a /dev/tcp/$host/$port pseudo device which covers almost all use cases of Netcat just in case your machine has no Netcat installed.
I still don't understand why such a device/file is not shipped with Unix/Linux by default so it would work with any shell or program.
- zaik 5y agoAre you sure this is a Bash feature and not some kernel module?
- FreeFull 5y agoIt definitely is a bash feature. From the bash manpage: Bash handles several filenames specially when they are used in redirections, as described in the following table. If the operating system on which bash is running provides these spe‐ cial files, bash will use them; otherwise it will emulate them internally with the behavior described below. /dev/fd/fd If fd is a valid integer, file descriptor fd is duplicated. /dev/stdin File descriptor 0 is duplicated. /dev/stdout File descriptor 1 is duplicated. /dev/stderr File descriptor 2 is duplicated. /dev/tcp/host/port If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open the corresponding TCP socket. /dev/udp/host/port If host is a valid hostname or Internet address, and port is an integer port number or service name, bash attempts to open the corresponding UDP socket.
- gnu8 5y agoIt just seems confusing that bash is using the /dev namespace for this feature. Are there circumstances where /dev/tcp/host/port might be provided by the operating system and provide the same functionality? Or is that just saying bash will stay out of the way if you are trying to access special files in /dev if they really exist?
- dpedu 5y agoUnless the operating system decides to use bash to access some device, it's fine. Nothing besides bash can see these. They're not really there. It's a mirage created by bash.
- FreeFull 5y ago/dev/std{in,out,err} are provided by Linux, and bash does stay out of the way there. I'm not aware of anything that provides /dev/tcp/host/port, although I think Plan 9 might have something similar. If the file happens to exist, bash will use the file rather than its own special functionality.
- zamadatix 5y agoThe old TLI/XTI alternatives to BSD sockets (pre when BSD sockets were standardized into POSIX sockets) back in the System V days provided this path/socket connection format. For a modern holdover these are still available in Solaris.
- tyingq 5y agoGood for clients, but bash can't do a listen socket. Gawk can, though it's pretty limited.
- AnIdiotOnTheNet 5y agoBecause Linux didn't copy everything from Plan9. Unfortunately.
- yjftsjthsd-h 5y agoI wonder how hard it would be to write kernel modules to fix that.
- anthk 5y agoaux/listen on Linux world? What's next? purging NFS for 9p and wireguard?
- tyingq 5y agoWSL uses 9p, though it seems to be a bit slow in that area.