Tuesday, June 5, 2012

How to trim leading whitespaces from a variable in Bash

Suppose your variable is PDNS where 


PDNS=" test " then to remove leading whitespaces from it try:


PDNS="${PDNS#"${PDNS%%[![:space:]]*}"}"
echo $PDNS


This will trim the leading whitespaces.


To remove trailing whitespaces from this variable try: 


PDNS="${PDNS%"${PDNS##*[![:space:]]}"}"

No comments:

Post a Comment