2

In terminal let's run..

a=' aa a '
b=`echo $a | sed -e 's/^ *//g' -e 's/ *$//g'`

i believe it removes empty spaces from beginning and ending of the script.
but preserves the empty space inbetween

aa a

but when i run this in a bash script.. it returns with

aaa

the empty space in-between string is removed too.

perhaps i need to escape something ?

  • It works fine for me as is. Afterwards I `echo \"$b\"` and get `"aa a"`. – paddy Aug 26 '13 at 00:13
  • @Tim Cooper, still removing the empty space between the string. –  Aug 26 '13 at 00:13
  • Are you setting `b` in script and then checking it outside the script? – paddy Aug 26 '13 at 00:16
  • Looks like it worked after i added $b into quotes. it was a command like this.. `xdotool type "$b"` –  Aug 26 '13 at 00:16
  • @paddy, i took your advice but decided to use quotes on $b after another comment also suggested \"$b\" –  Aug 26 '13 at 00:16
  • Now it's working basically. –  Aug 26 '13 at 00:17
  • 1
    @user2716439: it's working, but not the way you think. Because of the lack of quotes, `echo $a` is removing leading and trailing spaces before the string even gets to `sed`. It's also converting runs of whitespace (spaces, tabs, and newlines) into single spaces, expanding wildcards against files in the current directory... all probably things you won't want. Try `a=' a * aa '; echo $a` to see what I mean. *Always* put variable references in double-quotes unless there's a good reason not to. – Gordon Davisson Aug 26 '13 at 00:34
  • @GordonDavisson This is the correct answer, care to post it as such? – Adrian Frühwirth Aug 27 '13 at 09:13

2 Answers2

0

Try this :

a="   space a a   " 
b=$(echo $a | sed -e 's/^\ *|\ *$//g') 
echo "<$b>"

it will output :

<space a a>
Bertrand
  • 151
  • 1
  • 5
0

I'm not sure what's causing the final problem, but there's an earlier problem: referring to variables without double-quotes around them will cause unwanted parsing of the variable's value. For instance, the command echo $a will substitute the value of $a, then perform word splitting (which removes leasing and trailing whitespace, and converts whitespace within the variable into word breaks), then glob (aka wildcard) expansion if any of the words contain wildcards. (And then echo pastes the "words" it receives back together with a single space between them.) echo "$a", on the other hand, echoes the value of $a intact:

$ a='    a    *    aa    '   # Note multuple spaces and a wildcard
$ echo $a "end"    # "end" is there so you can tell if the trailing spaces are echoed
a file1.txt file2.txt file3.pdf aa end
$ echo "$a" "end"
    a    *    aa     end

As a result of this, in b=$(echo $a | sed -e 's/^\ *|\ *$//g') the leading and trailing spaces in $a are removed before the string even gets to sed. This would be fine if that were the only thing that'd been done, but as you saw above the string has been messed with in other (undesirable) ways as well.

As far as I can see, the sed command will do exactly what it should, as long as you pass it the unmangled value of $a:

$ b=$(echo "$a" | sed -e 's/^ *//g' -e 's/ *$//g')
$ echo "$b" "end"    # Note that echo will put a single space between $b and "end"
a    *    aa end
$ echo "'$b'"    # Here's a more direct way to mark the beginning and end of the string
'a    *    aa'

... so it looks to me that something else (probably after the quoted code) is messing with the value of '$b'. Possibly it's being also used without the benefit of double-quotes, and that's causing trouble.

Gordon Davisson
  • 118,432
  • 16
  • 123
  • 151