# Taken from: http://www.jlaforums.com/viewtopic.php?t=1427403 # Author: Chris F.A. Johnson var=' space test ' echo $var | sed -n -e 'l' echo "$var" | sed -n -e 'l' printf "%s" "${var#"${var%%[![:space:]]*}"}" | sed -n -e 'l' printf "%s" "${var%"${var##*[![:space:]]}"}" | sed -n -e 'l' # Space characters include: tab, newline, vertical tab, form feed, carriage return, and space. # cf. "POSIX character classes" at http://en.wikipedia.org/wiki/Regular_expression var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo "$var" | sed -n -e 'l'
Never been to CodeSnippets before?
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world (or not, you can keep them private!)
Remove leading & trailing whitespace from a Bash variable (See related posts)
You need to create an account or log in to post comments to this site.
Related Posts
» add2path in shell osx mac bash unix add remove variable path
» Using external variables in awk in awk shell osx mac bash unix variable external
» ws - web search from the com... in shell osx mac search bash unix commandline web
» snippet in shell osx mac html bash unix commandline web download convert textutil ed snippet
» Switching firewall settings ... in shell osx mac bash unix ipfw firewall ruleset switch
» Delete WebKit search fields in shell osx mac bash unix safari webkit
Snippets (source code soon to be available) developed by Peter Cooper and powered by Ruby On Rails