Bash snippet - Trim leading and trailing whitespace from a variable.


SUBMITTED BY: DanielK

DATE: Aug. 13, 2016, 2:32 p.m.

FORMAT: Bash

SIZE: 178 Bytes

HITS: 741

  1. # Usage: trim_whitespace some_var
  2. trim_whitespace()
  3. {
  4. local trim_wh
  5. local to_trim
  6. eval to_trim=\$$1
  7. read -rd '' trim_wh <<< "$to_trim"
  8. eval $1=\$trim_wh
  9. }

comments powered by Disqus