Bash itself has string manipulation procedures.
Try:
s='https://hello.world.example.com'
s1="${s#*//}" # Deletes shortest match of *// from front of $s
s2="${s1%%.*}" # Delete longest match of .* (ie, from first .) in s1
echo "$s2"
# hello
In the first case, #*//
is the shortest match from the front of the string up to and including //
.
In the second case, %%.*
matches the longest sub string from the literal '.' to the end of the string represented by *
You can read more here.
Or use sed
:
echo "$s" | sed -E 's/^[^/]*\/\/([^.]*)\..*/\1/'
^ Substitute
^ Start of string
^ Not a /
^ Two literal //
^ Capture that
^ Literal .
^ The rest of the string
^ The replacement
is the captured word