Bash & Shell Scripting (50 Qs)•Part 3: Advanced Text Processing & Production Internals (Senior / Lead)
TCP Sockets in Bash: Native network reads/writes using `/dev/tcp/host/port` without curl or netcat.
senior LevelCode Examplebash-shellAdvanced Text Processing
In Simple Words: Bash includes built-in TCP/UDP socket abstraction via virtual paths:
bash
# Check if a remote port is open without netcat
if (echo > /dev/tcp/db.example.com/5432) 2>/dev/null; then
echo "Database port is reachable"
else
echo "Database connection failed"
fi
# Send raw HTTP GET request
exec 3<>/dev/tcp/httpbin.org/80
echo -e "GET /ip HTTP/1.1
Host: httpbin.org
Connection: close
" >&3
cat <&3
exec 3>&- # Close socket