# usage: VAR=$(trim " string ") trim() { local var=$1 var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } # Inline version of trim, use when piping itrim () { sed -r 's/^\s+//g;s/\s+$//g' } # usage: CANONICALIZED_STRING=$(canonalize ) # usage with relative path requires you to be in the correct directory. canonicalize() { cd -P -- "$(dirname -- "$1")" && printf '%s\n' "$(pwd -P)/$(basename -- "$1")" } escape_search() { sed -e 's/[]\/()$*.^|[]/\\&/g' } escape_replace() { sed -e 's/[\/&]/\\&/g' }