3 ms·
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;
by peterwwillis 7y ago
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.
- jwilk 7y agoUnlike the original code, this strips whitespace from every line of input.
- jolmg 7y ago> 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).
- jwilk 7y agoBeware that -r is a GNU-specific option.