How to Extract a Domain Name from a Full URL in BASH?


To extract the domain name from a full URL in a Bash script, you can use parameter expansion, sed, awk, or grep. Each method has its own benefits, and which one you use may depend on your specific circumstances or personal preference. Below are examples of each method:

Using Parameter Expansion to Extract a Domain Name from a Full URL in BASH

Bash parameter expansion is a built-in feature and doesn’t require calling an external program like sed, awk, or grep. However, it’s a bit less readable for those not familiar with its syntax.

1
2
3
4
5
6
7
8
#!/bin/bash
 
url="https://www.google.com:8080/asb/c"
protocol_removed="${url#*://}"      # remove the protocol
path_removed="${protocol_removed%%/*}"  # remove the path
port_removed="${path_removed%%:*}"     # remove the port
 
echo "Domain name: $port_removed"
#!/bin/bash

url="https://www.google.com:8080/asb/c"
protocol_removed="${url#*://}"      # remove the protocol
path_removed="${protocol_removed%%/*}"  # remove the path
port_removed="${path_removed%%:*}"     # remove the port

echo "Domain name: $port_removed"

Here is the wrapped BASH Function to get the domain from the URL string using the parameter expansion in BASH:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
 
extract_domain_param() {
    local url="$1"
    local protocol_removed="${url#*://}"
    local path_removed="${protocol_removed%%/*}"
    local port_removed="${path_removed%%:*}"
    echo "$port_removed"
}
 
# Usage
url="https://www.google.com:8080/asb/c"
domain=$(extract_domain_param "$url")
echo "Domain name: $domain"
#!/bin/bash

extract_domain_param() {
    local url="$1"
    local protocol_removed="${url#*://}"
    local path_removed="${protocol_removed%%/*}"
    local port_removed="${path_removed%%:*}"
    echo "$port_removed"
}

# Usage
url="https://www.google.com:8080/asb/c"
domain=$(extract_domain_param "$url")
echo "Domain name: $domain"

Using sed to Extract a Domain Name from a Full URL in BASH

The sed command (stream editor) is a powerful and flexible command used to perform basic text transformations on an input stream (a file or input from a pipeline).

1
2
3
4
5
6
#!/bin/bash
 
url="https://www.google.com:8080/asb/c"
domain=$(echo $url | sed -e 's|^[^/]*//||' -e 's|/.*$||' -e 's|:.*$||')
 
echo "Domain name: $domain"
#!/bin/bash

url="https://www.google.com:8080/asb/c"
domain=$(echo $url | sed -e 's|^[^/]*//||' -e 's|/.*$||' -e 's|:.*$||')

echo "Domain name: $domain"

And wrapped in a BASH Function:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
 
extract_domain_sed() {
    local url="$1"
    echo "$url" | sed -e 's|^[^/]*//||' -e 's|/.*$||' -e 's|:.*$||'
}
 
# Usage
url="https://www.google.com:8080/asb/c"
domain=$(extract_domain_sed "$url")
echo "Domain name: $domain"
#!/bin/bash

extract_domain_sed() {
    local url="$1"
    echo "$url" | sed -e 's|^[^/]*//||' -e 's|/.*$||' -e 's|:.*$||'
}

# Usage
url="https://www.google.com:8080/asb/c"
domain=$(extract_domain_sed "$url")
echo "Domain name: $domain"

Using awk to Extract a Domain Name from a Full URL in BASH

awk is a powerful language used for pattern scanning and processing. It makes it easy to split string content based on delimiters.

1
2
3
4
5
6
#!/bin/bash
 
url="https://www.google.com:8080/asb/c"
domain=$(echo $url | awk -F[/:] '{print $4}')
 
echo "Domain name: $domain"
#!/bin/bash

url="https://www.google.com:8080/asb/c"
domain=$(echo $url | awk -F[/:] '{print $4}')

echo "Domain name: $domain"

And here is the BASH Function to implement this method:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
 
extract_domain_awk() {
    local url="$1"
    echo "$url" | awk -F[/:] '{print $4}'
}
 
# Usage
url="https://www.google.com:8080/asb/c"
domain=$(extract_domain_awk "$url")
echo "Domain name: $domain"
#!/bin/bash

extract_domain_awk() {
    local url="$1"
    echo "$url" | awk -F[/:] '{print $4}'
}

# Usage
url="https://www.google.com:8080/asb/c"
domain=$(extract_domain_awk "$url")
echo "Domain name: $domain"

Using grep with Perl-compatible regex (PCRE) to Extract a Domain Name from a Full URL in BASH

grep is a command-line utility for searching plain-text data for lines that match a regular expression. When using the -P option, grep interprets the pattern as a Perl regular expression.

1
2
3
4
5
6
#!/bin/bash
 
url="https://www.google.com:8080/asb/c"
domain=$(echo $url | grep -oP '(?<=://)[^:/]+')
 
echo "Domain name: $domain"
#!/bin/bash

url="https://www.google.com:8080/asb/c"
domain=$(echo $url | grep -oP '(?<=://)[^:/]+')

echo "Domain name: $domain"

The BASH Function to obtain the domain part of a given URL string using the grep:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/bash
 
extract_domain_grep() {
    local url="$1"
    echo "$url" | grep -oP '(?<=://)[^:/]+'
}
 
# Usage
url="https://www.google.com:8080/asb/c"
domain=$(extract_domain_grep "$url")
echo "Domain name: $domain"
#!/bin/bash

extract_domain_grep() {
    local url="$1"
    echo "$url" | grep -oP '(?<=://)[^:/]+'
}

# Usage
url="https://www.google.com:8080/asb/c"
domain=$(extract_domain_grep "$url")
echo "Domain name: $domain"

Conclusion: BASH Function to Extract Domain from URL

We have covered a few methods to extract the domain part from a given URL string using the BASH: sed, awk, grep and parameter expansion. We also implement a few BASH Functions to parse the URL and obtain the component of the domain, but it should be easy to also parse into other components such as port numbers or path string.

Remember to make your script executable with chmod +x scriptname.sh and then run it with ./scriptname.sh. Each of these snippets assumes that you’re providing a well-formed URL and doesn’t account for every possible edge case in URL formatting. They’re intended to handle standard, well-structured URLs and may not work with malformed ones.

Each function is designed to receive the URL as an argument and print the domain to standard output. You can then capture this output in a variable using command substitution $(…) in your script.

To use these functions, you’d source this script file from your main script or copy these functions into your main script file. After declaring the functions, you can call them with any URL as demonstrated in the usage examples. Remember, these scripts assume well-formed URLs and may not handle every edge case or malformed URL.

BASH Programming/Shell

–EOF (The Ultimate Computing & Technology Blog) —

GD Star Rating
loading...
934 words
Last Post: How to Kill a Process That Opens a TCP/UDP Port?
Next Post: Teaching Kids Programming - Number of Ways to Stay in the Same Place After Some Steps (Top Down Dynamic Programming Algorithm, Recursion with Memoization)

The Permanent URL is: How to Extract a Domain Name from a Full URL in BASH?

Leave a Reply