EN
Bash - get protocol name from given URL
0
points
In this article, we would like to show you how to get protocol name from given URL in Bash.
Quick solution:
echo 'https://dirask.com/about' | awk 'match($0,/^([a-z]+):\/\/(www\.)?[^:?#/]+/,g){print g[1]}'
Practical example
In this solution, we use 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[1]}'
echo 'https://dirask.com/about' | awk 'match($0,/^([a-z]+):\/\/(www\.)?[^:?#/]+/,g){print g[1]}'
echo 'https://www.dirask.com' | awk 'match($0,/^([a-z]+):\/\/(www\.)?[^:?#/]+/,g){print g[1]}'
echo 'https://www.dirask.com/about' | awk 'match($0,/^([a-z]+):\/\/(www\.)?[^:?#/]+/,g){print g[1]}'
Output:
https
https
https
https
Reusable example
#!/bin/bash
function get_protocol() {
echo "$1" | awk 'match($0,/^([a-z]+):\/\/(www\.)?[^:?#/]+/,g){print g[1]}'
}
# Usage example 1:
get_protocol 'https://dirask.com'
get_protocol 'https://dirask.com/about'
get_protocol 'https://www.dirask.com'
get_protocol 'https://www.dirask.com/about'
# Usage example 2:
protocol_1="$(get_protocol 'https://dirask.com')"
protocol_2="$(get_protocol 'https://dirask.com/about')"
protocol_3="$(get_protocol 'https://www.dirask.com')"
protocol_4="$(get_protocol 'https://www.dirask.com/about')"
echo $protocol_1
echo $protocol_2
echo $protocol_3
echo $protocol_4
Output:
https
https
https
https
https
https
https
https