Skip to content

🔗 url

A library of URL related functions.

lb_get_domain url

Return the domain from a url.

Parameters:
Name Type Description Default
url string The url from which to get the domain required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/url"

function main() {
  url="http://user:[email protected]:80/some/path/url?data&and#hash"
  printf "url: %s\n" "${url}"

  s=$(lb_get_domain "${url}")
  printf "lb_get_domain: %s\n" "${s}"

  s=$(lb_url_encode "${url}")
  printf "lb_url_encode: %s\n" "${s}"

  url="http%3A%2F%2Fuser%3Apw%40example.com%3A80%2Fsome%2Fpath%2Furl%3Fdata%26and%23hash"
  printf "url: %s\n" "${url}"

  s=$(lb_url_decode "${url}")
  printf "lb_url_decode: %s\n" "${s}"
}

main "${@}"
Show source code in url
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

function lb_get_domain() {
  lb_check_args_num "${@}" 1
  # https://unix.stackexchange.com/a/428990/93726
  echo "${1}" | sed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/" | sed "s/^www\.//" | cut -f 1 -d '.'
}

function lb_url_encode(){
  lb_check_args_num "${@}" 1
  local LC_ALL=C
  for (( i = 0; i < ${#1}; i++ )); do
    : "${1:i:1}"
    case "$_" in
      [a-zA-Z0-9.~_-])  printf '%s' "$_";;
      *)                printf '%%%02X' "'$_";;
    esac
  done
  printf '\n'
}

function lb_url_decode() {
  lb_check_args_num "${@}" 1
  : "${1//+/ }"
  printf '%b\n' "${_//%/\\x}"
}

lb_url_encode url

Return a percent-encoded url.

Parameters:
Name Type Description Default
url string The url to encode required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/url"

function main() {
  url="http://user:[email protected]:80/some/path/url?data&and#hash"
  printf "url: %s\n" "${url}"

  s=$(lb_get_domain "${url}")
  printf "lb_get_domain: %s\n" "${s}"

  s=$(lb_url_encode "${url}")
  printf "lb_url_encode: %s\n" "${s}"

  url="http%3A%2F%2Fuser%3Apw%40example.com%3A80%2Fsome%2Fpath%2Furl%3Fdata%26and%23hash"
  printf "url: %s\n" "${url}"

  s=$(lb_url_decode "${url}")
  printf "lb_url_decode: %s\n" "${s}"
}

main "${@}"
Show source code in url
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

function lb_get_domain() {
  lb_check_args_num "${@}" 1
  # https://unix.stackexchange.com/a/428990/93726
  echo "${1}" | sed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/" | sed "s/^www\.//" | cut -f 1 -d '.'
}

function lb_url_encode(){
  lb_check_args_num "${@}" 1
  local LC_ALL=C
  for (( i = 0; i < ${#1}; i++ )); do
    : "${1:i:1}"
    case "$_" in
      [a-zA-Z0-9.~_-])  printf '%s' "$_";;
      *)                printf '%%%02X' "'$_";;
    esac
  done
  printf '\n'
}

function lb_url_decode() {
  lb_check_args_num "${@}" 1
  : "${1//+/ }"
  printf '%b\n' "${_//%/\\x}"
}

lb_url_decode url

Return a percent-decoded url.

Parameters:
Name Type Description Default
url string The url to decode required
Examples:
#!/usr/bin/env bash

# shellcheck source=/dev/null
source ../init
source "${LIBBASH_DIR}/url"

function main() {
  url="http://user:[email protected]:80/some/path/url?data&and#hash"
  printf "url: %s\n" "${url}"

  s=$(lb_get_domain "${url}")
  printf "lb_get_domain: %s\n" "${s}"

  s=$(lb_url_encode "${url}")
  printf "lb_url_encode: %s\n" "${s}"

  url="http%3A%2F%2Fuser%3Apw%40example.com%3A80%2Fsome%2Fpath%2Furl%3Fdata%26and%23hash"
  printf "url: %s\n" "${url}"

  s=$(lb_url_decode "${url}")
  printf "lb_url_decode: %s\n" "${s}"
}

main "${@}"
Show source code in url
#!/usr/bin/env bash
: "${LIBBASH_DIR:?LIBBASH_DIR must be set. Please source libbash/init before other libraries.}"

#------------------------------ Global Variables ------------------------------

#------------------------------ Private Functions -----------------------------

#------------------------------ Public Functions ------------------------------

function lb_get_domain() {
  lb_check_args_num "${@}" 1
  # https://unix.stackexchange.com/a/428990/93726
  echo "${1}" | sed -e "s/[^/]*\/\/\([^@]*@\)\?\([^:/]*\).*/\2/" | sed "s/^www\.//" | cut -f 1 -d '.'
}

function lb_url_encode(){
  lb_check_args_num "${@}" 1
  local LC_ALL=C
  for (( i = 0; i < ${#1}; i++ )); do
    : "${1:i:1}"
    case "$_" in
      [a-zA-Z0-9.~_-])  printf '%s' "$_";;
      *)                printf '%%%02X' "'$_";;
    esac
  done
  printf '\n'
}

function lb_url_decode() {
  lb_check_args_num "${@}" 1
  : "${1//+/ }"
  printf '%b\n' "${_//%/\\x}"
}