Shell – Extracting substring from environment variable

shell-scriptstringurlvariable

In a bash or zsh script, how might I extract the
host from a url, e.g. unix.stackexchange.com from
http://unix.stackexchange.com/questions/ask, if the latter is in an environment variable?

Best Answer

You can use parameter expansion, which is available in any POSIX compliant shell.

$ export FOO=http://unix.stackexchange.com/questions/ask
$ tmp="${FOO#*//}" # remove http://
$ echo "${tmp%%/*}" # remove everything after the first /
unix.stackexchange.com

A more reliable, but uglier method would be to use an actual URL parser. Here is an example for python:

$ echo "$FOO" | python -c 'import urlparse; import sys;  print urlparse.urlparse(sys.stdin.read()).netloc' 
unix.stackexchange.com
Related Question