EN
Bash - get domain name from given URL
9
points
In this article, we would like to show how to get a domain from a given URL in Bash.
In the example, the solution uses awk according to good availability on multiple platforms.
Quick solution:
echo 'https://dirask.com/about' | awk 'match($0,/^[a-z]+:\/\/(www\.)?([^:?#/]+)/,g){print g[2]}'
or:
echo 'https://dirask.com/about' | sed -e 's|^[^/]*//||' -e 's|^www\.||' -e 's|/.*$||'
Practical example
Used solution calls match() function that lets to use regular expression with groups support.
#!/bin/bash
echo 'https://dirask.com' | awk 'match($0,/^[a-z]+:\/\/(www\.)?([^:?#/]+)/,g){print g[2]}'
echo 'https://dirask.com/about' | awk 'match($0,/^[a-z]+:\/\/(www\.)?([^:?#/]+)/,g){print g[2]}'
echo 'https://www.dirask.com' | awk 'match($0,/^[a-z]+:\/\/(www\.)?([^:?#/]+)/,g){print g[2]}'
echo 'https://www.dirask.com/about' | awk 'match($0,/^[a-z]+:\/\/(www\.)?([^:?#/]+)/,g){print g[2]}'
Output:
dirask.com
dirask.com
dirask.com
dirask.com
Reusable example
#!/bin/bash
function get_domain() {
echo "$1" | awk 'match($0,/^[a-z]+:\/\/(www\.)?([^:?#/]+)/,g){print g[2]}'
}
# Usage example 1:
get_domain 'https://dirask.com'
get_domain 'https://dirask.com/about'
get_domain 'https://www.dirask.com'
get_domain 'https://www.dirask.com/about'
# Usage example 2:
domain_1="$(get_domain 'https://dirask.com')"
domain_2="$(get_domain 'https://dirask.com/about')"
domain_3="$(get_domain 'https://www.dirask.com')"
domain_4="$(get_domain 'https://www.dirask.com/about')"
echo $domain_1
echo $domain_2
echo $domain_3
echo $domain_4
Output:
dirask.com
dirask.com
dirask.com
dirask.com
dirask.com
dirask.com
dirask.com
dirask.com