I have the following URLs with different types of protocol:
URL=https://example.com
URL=ftp://example.com
URL=ssh://example.com
URL=///filestore/storage
I'm using echo ${URL%://*}
to get protocol, but for ///
it doesn't work, can't apply multiple regex, please help
You can use this extended glob matching:
"${url%?(:)//*}"
Where ?(:)
matches an optional :
using extended globbing followed by //
and that is followed by any number of characters as per glob matching in shell.
Code:
for url in 'https://example.com' 'ftp://example.com' 'ssh://example.com' '///filestore/storage'; do
echo "${url%?(:)//*}"
done
https
ftp
ssh
/
PS: On BASH versions < 4 use shopt -s extglob
before this code.