I will set some variables and then extract out portions of the variable employing Bash shell variable interpretations.
t1 originally >< ${t1:-notSet} returns >notSet< t1 now >< t2 originally >< ${t2:=nowSet} returns >nowSet< t2 now >nowSet< t3 originally >setBefore< ${t3:?Error variable not set} returns >setBefore< t3 now >setBefore< If t3 had not been set script would have died with message Error variable not set ${t3:?Error variable not set} t4 originally >ll< ${t4:+alreadySet} returns >alreadySet< t4 now >ll< t5 /export/home/shinchey/this.is.a.test ${t5:8:4} substr from 8 for 4 chars ->home ${#t5} length of variable t4 ->36 ${t5##*/} extract file name ->this.is.a.test ${t5%/*} extract directory name ->/export/home/shinchey ${t5#/*/} down one level ->home/shinchey/this.is.a.test ${t5%.*} drop one suffix ->/export/home/shinchey/this.is.a ${t5%%.*} drop all suffixes ->/export/home/shinchey/this ${t5//./} remove all periods ->/export/home/shinchey/thisisatest name ->ABC-123-wxy ${name%%-*} 1st part only ->ABC tt=${name#*-} ${tt%-*} 2nd part only ->123 ${name##*-} 3rd part only ->wxy ${name#*-} no 1st part ->123-wxy ${name%%-*}-${name##*-} no 2nd part ->ABC-wxy ${name%-*} no 3rd part ->ABC-123
Copyright © 1997 - 2020, Stephen Hinchey