summaryrefslogtreecommitdiffstats
path: root/helper/string.inc
blob: 0a1667082d8b08ecb6b60f04ed3daed8a2732634 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14

# 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"
}

# usage: CANONICALIZED_STRING=$(canonalize <path>)
#        usage with relative path requires you to be in the correct directory.
canonicalize() {
	cd -P -- "$(dirname -- "$1")" && printf '%s\n' "$(pwd -P)/$(basename -- "$1")"
}