tags:

views:

5071

answers:

3

Assuming var contains spaces, newlines, and tabs followed by some text,

why does

${var#"${var%%[![:space:]]*}"}  # strip var of everything 
                                # but whitespace
                                # then remove what's left 
                                # (i.e. the whitespace) from var

remove the white space and leave the text but

${var##[:space:]*}  # strip all whitespace from var

doesn't?

+3  A: 

[:space:] is a character class. It's only valid if it is nested inside another set of [ ].

Barry Kelly
Thanks - I didn't know that character classes required nested brackets.
MCS
@MCS - This is documented in regex(7) ("man 7 regex" to read it). Most modern regex implementations include support for the POSIX named character classes, so you can read about them in perl's perlre man page, for example, as well.
converter42
+7  A: 

If I set var=" This is a test ", both your suggestions do not work; just the leading stuff is removed. Why not use the replace functionality that removes all occurrences of whitespace and not just the first:

 ${var//[[:space:]]}
flolo
A: 

flolo's answer is documented in the "Parameter Substitution" section of the bash man page. Another source of documentation is the Parameter Substitution section of the Advanced Bash-Scripting Guide. The ABS guide includes basic documentation with excellent example code.

converter42