Bash Trim Whitespace

Tags: May 2, 2014 10:45 AM

To remove leading and trailing whitespace from a subset of strings in a shell we can use sed.

$ echo '   #FOO#   ' | sed -e 's/\s*$//' -e 's/^\s*//'

Function 'trim'

To reuse it in another place, it is good idea to wrap it as a function.
# Function to trim leading and trailing spaces
trim() {
  # Accept input from argument or STDIN
  # So you can do both:
  # $ echo '  #FOO#   ' | trim
  # or
  # $ trim '   #FOO#   '
  local STRING=$( [ ! -z "$1" ] && echo $1 || cat ; )
  echo "$STRING" | sed -e 's/^\s*//' -e 's/\s*$//'
Now it can be used to trim a string both from argument or standard input.
$ echo '   #FOO#   ' | trim

$ trim '   #FOO#   '


Share on Facebook Twitter


Anonymous said...

Placing inside bets is both deciding on the precise variety of the pocket the ball will land in, or a small range of pockets based on their 1xbet proximity on the layout. Players wishing to guess on the 'outside' will choose bets on bigger positional groupings of pockets, the pocket colour, or whether the successful number is odd or even. The payout odds for each kind of guess are based on its likelihood.

Post a Comment