Skip to content

Instantly share code, notes, and snippets.

@gervaisb
Created August 10, 2021 09:30
Show Gist options
  • Save gervaisb/d8f7ae23dc4994abc53b7bca31c2d91b to your computer and use it in GitHub Desktop.
Save gervaisb/d8f7ae23dc4994abc53b7bca31c2d91b to your computer and use it in GitHub Desktop.
A wrapper over the MacOs hostname command to accept the '-i' or '--ip-address' flag.
#!/bin/sh
# hostname.sh - a wrapper over the MacOs hostname command to accept the '-i'
# or '--ip-address' flag.
#
# SYNOPSIS
# hostname [-i|--ip-address] [*]
#
# OPTIONS
# -i, --ip-address
# Display the IP address(es) of the host.
# *
# Delegate everything to '/bin/hostname'.
get_ip_adress () {
# This is a naive implementation that assume there is
# only one active network device.
ifconfig en0 | awk '/inet / {print $2; }'
exit 0
}
delegate () {
/bin/hostname "$@"
}
if [ $1 == '-i' ] || [ $1 == '--ip-address' ]
then
get_ip_adress
else
delegate "$@"
fi
@gervaisb
Copy link
Author

I missed the hostname -i command too much and created this naive wrapper that can print my IP address.

To use it I have aliased it to hostname:

alias hostname="/Users/me/hostname.sh"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment