News for hackers. Stuff that matters.
Re:Pure Bash Bible by peterwwillis on Thursday September 19, 2019 @10:12AM (#21016329)
Is the following a maintenance headache, or non-human readable?

  #!/bin/sh
  FOO=" some long string here "
  FOO="$( echo "$FOO" | sed -e ' s/^[[:space:]]//g; s/[[:space:]]$//g ' )"
This isn't "pure bash", but most of what I write in shell scripts isn't "pure bash". It's shell scripting: dirty, slow, easy, effective.

Like any 'language', it takes on the complexity you put into it. English is really complicated, but you can also use a subset of it with only 850, 1200, or 2400 words, and suddenly it's very simple and clear.

Re:Pure Bash Bible by jolmg on Thursday September 19, 2019 @01:24PM (#21018420)
> echo "$FOO" | sed -e ' s/^[[:space:]]//g; s/[[:space:]]$//g '

Your example fails when $FOO is "-n", for instance. Also the g modifiers are redundant in your example since there's only one beginning of line per line and one end of line per line.

I would instead write this:

  sed -r 's/^\s+|\s+$//g' <<< "$FOO"
EDIT: I think I see now what you probably thought would happen by using g, but no it wouldn't remove multiple spaces. So, you example also fails when $FOO is " x" (using 2 or more spaces at the ends).
Re:Pure Bash Bible by jwilk on Friday September 20, 2019 @03:13PM (#21030190)
Beware that -r is a GNU-specific option.
Re:Pure Bash Bible by jwilk on Friday September 20, 2019 @03:10PM (#21030165)
Unlike the original code, this strips whitespace from every line of input.