bash substring

NAME=${MYVAR%:*}  # retain the part before the colon

NAME=${NAME##*/} # retain the part after the last slash

 
${MYVAR#pattern}     # delete shortest match of pattern from the beginning
${MYVAR##pattern}    # delete longest match of pattern from the beginning
${MYVAR%pattern}     # delete shortest match of pattern from the end
${MYVAR%%pattern}    # delete longest match of pattern from the end 
 
   

So # means match from the beginning (think of a comment line) and % means from the end. One instance means shortest and two instances means longest.

 

${MYVAR:3}   # Remove the first three chars (leaving 4..end)
${MYVAR::3}  # Return the first three characters
${MYVAR:3:5} # The next five characters after removing the first 3 (chars 4-9) 
 
${MYVAR/search/replace}