You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
## syntax ##
${parameter:offset:length}
${a:1} # everything after position 1
${a:0:5} # offset 0 with length 5
## syntax ##
${parameter:-default} # set a default if $parameter udefined
${a:-$b} # output: ${a} ? ${a} : ${b}
Substring Replacement
# Parameter expansion with substring replacement
# It takes the existing variable username in `=${username` then will replace every occurrence `//` of `*= ` with `/...` (nothing).
# NOTE: the closing '/' is implied
username=${username//*= /}
Substring Removal
# Deletes shortest match of $substring from FRONT of $string.
${string#substring}
# Deletes longest match of $substring from FRONT of $string.
${string##substring}
# Deletes shortest match of $substring from BACK of $string.
${string%substring}
# Deletes longest match of $substring from BACK of $string.
${string%%substring}
# Find and remove (replace with nothing) 'chars followed by any number of zeroes'.
echo "$TEST" | sed 's/chars0*//'
Only print the actual match (not the entire matching line), use a substitution.
# Replace whatever comes before and after the match with nothing, then print the whole line (p)
sed -n 's/.*\([0-9][0-9]*G[0-9][0-9]*\).*/\1/p'