Shell – Why can I not extract the first word (size) from output of “du” command with ${xx%% *}

shell

I just wanted to write a script and needed to extract the size of directories. There I discovered a strange effect which I don't understand:

I used the "du" command:

> x=$(du mydir)
> echo $x
8192 mydir

So far ok. But now I want to extract the size by removing all chars from x beginning with the first whitespace. But then I get

> echo ${x%% *}
8192 mydir

instead of just 8192.

So checked that with another variable, not generated by "du"

> y="8192 mydir"
> echo ${y%% *}
8192

Why does this work for y but not for x?
I also checked that x and y are identical strings.

I'm really puzzled here. I will be very pleased if anybody has an answer for that?

Best Answer

If you double-quote your variable you'll see that there's visibly more than one space. In actual fact it's a tab character, which you can see on some systems more clearly with

echo "$x" | cat -e

You can therefore match the tab (or space) with this construct, which removes the longest sequence of "space-or-tab followed by anything" that's bound to the end of the string value in $x

echo "${x%%[[:blank:]]*}"