remove whitespace from bash variable

Assuming a variable contains spaces, newlines, and tabs followed by some text, why does this:

${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 this:

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

does not?

Answers


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:]]}

Need Your Help

find/highlight common substrings between two buffers in Emacs

emacs substrings

Can somebody tell me how to highlight common substrings between two buffers in Emacs?

Bug in CSS3 rotateY transition on Safari?

css3 safari css-transitions

I am showing a modal popup using CSS3 transitions (largely borrowed from Effeckt.css). It works well in all modern browsers except Safari. In Safari, the movement is OK, but the background-color sn...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.