How do I extract the domain name from a url using bash? like: http://example.com/ to example.com must work for any tld, not just .com
basename "http://example.com"
Now of course, this won't work with a URI like this: http://www.example.com/index.html but you could do the following:
basename $(dirname "http://www.example.com/index.html")
Or for more complex URIs:
echo "http://www.example.com/somedir/someotherdir/index.html" | cut -d'/' -f3
-d means "delimiter" and -f means "field"; in the above example, the third field delimited by the forward slash '/' is www.example.com.