Search This Blog

2024-06-14

trim in pure dash

SPACES=$(printf ' \n\t')  # '\n' must not be last

trim () {

  local trim="${1#*[^$SPACES]}"

  local head=${1%?$trim}

  trim="${1%[^$SPACES]*}"

  local tail=${1#$trim?}

  # trim

  trim="${1#$head}"

  echo -n "${trim%$tail}"
}

# Test

trim '

hello

world

'