Friday, March 4, 2011

remove whitespace from bash variable

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?

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

    MCS : Thanks - I didn't know that character classes required nested brackets.
    converter42 : @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.
  • 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'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.

0 comments:

Post a Comment