Skip to content

Useful Bash Snippets

These are a collection of bash snippets I’ve written over the years.

Get machine’s gateway

gateway=$(netstat -rn | grep 0.0.0.0 | awk '{print $2}' | grep -v "0.0.0.0")

Get script’s directory

setup_dir=$(dirname $(readlink -f $0))

Get the current date and time (YYYYMMDD-HHMMSS)

now=$(date +"%Y%m%d-%H%M%S")

Get the active network card

wan_interface=$(route | grep '^default' | grep -o '[^ ]*$')

Get machine’s LAN IP

lan_ip=$(hostname -I | cut -d ' ' -f1)

Get machine’s WAN IP

wan_ip=$(curl "https://icanhazip.com")