Skip to content

Instantly share code, notes, and snippets.

@Thavarshan
Created February 12, 2025 03:55
Show Gist options
  • Save Thavarshan/e5a8cb5792a374853f1da2f5d541300f to your computer and use it in GitHub Desktop.
Save Thavarshan/e5a8cb5792a374853f1da2f5d541300f to your computer and use it in GitHub Desktop.
This function fetches your public IP using https://api.ipify.org, displays it, and copies it to the clipboard if possible. It supports macOS (pbcopy), Linux with xclip, and Linux with xsel. πŸš€
myip() {
local ip=$(curl -s https://api.ipify.org) # Fetch IPv4 address
echo "Your public IP (IPv4) is: $ip"
# Copy to clipboard based on OS
if command -v pbcopy &>/dev/null; then
echo -n "$ip" | pbcopy # macOS
echo "Copied to clipboard!"
elif command -v xclip &>/dev/null; then
echo -n "$ip" | xclip -selection clipboard # Linux with xclip
echo "Copied to clipboard!"
elif command -v xsel &>/dev/null; then
echo -n "$ip" | xsel --clipboard --input # Linux with xsel
echo "Copied to clipboard!"
else
echo "Clipboard copy not supported on this system."
fi
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment