Skip to content

Instantly share code, notes, and snippets.

@vvkpd
Last active January 30, 2019 08:18
Show Gist options
  • Save vvkpd/472ffe4bdda5512554ab35202549e31f to your computer and use it in GitHub Desktop.
Save vvkpd/472ffe4bdda5512554ab35202549e31f to your computer and use it in GitHub Desktop.
Basic regex operations in shell
url='https://guide.bash.academy/variables.html'
echo "Sample URL"
echo "-----------------$url-----------------";
# Remove the shortest string that matches the pattern if it's at the start of the value.
echo "${url#*/}"
# Remove the longest string that matches the pattern if it's at the start of the value.
echo "${url%/*}"
# Remove the shortest string that matches the pattern if it's at the end of the value.
echo "${url##*/}"
# Remove the longest string that matches the pattern if it's at the end of the value.
echo "${url%%/*}"
# Replace the first string that matches the pattern with the replacement.
echo "${url/./-}"
# Replace each string that matches the pattern with the replacement.
echo "${url//./-}"
# Replace the string that matches the pattern at the beginning of the value with the replacement.
echo "${url/#*:/http:}"
# Replace the string that matches the pattern at the end of the value with the replacement.
echo "${url/%.html/.jpg}"
# Expand the length of the value (in bytes).
echo "${#url}"
# Expand a part of the value, starting at start, length bytes long.
echo "${url:7}"
# name of the script
echo "$0"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment