Languages
[Edit]
EN

Bash - get domain name from given URL

9 points
Created by:
Nataniel-Barclay
499

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

 

See also

  1. Bash - get protocol name from given URL

  2. Bash - get path name from given URL

  3. Bash - get location origin from given URL

Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join