To remove spaces and tabs from left to first word, enter: One common problem I have personally made and seen others make while shell scripting is trying to set a variable to be the contents of a file or the output of a multiline command, and then trying to echo the variable to process it further with grep, awk, a while loop, etc. A simple answer is: I wrote a sed solution that only uses a single expression rather than two: The trim function's parameter is a variable name: Still an excelente solution, Vamp.
To generalize the solution to handle all forms of whitespace, replace the space character in the tr and sed commands with [[:
Trim the whitespace characters from a Bash variable
On Cygwin difference in speed is orders of magnitude. Removing all white-space from a string is not same as removing both leading and trailing spaces as in question. In order to remove all the spaces from the beginning and the end of a string including end of line characters: Do you want to delete whitespaces or tabs? Email Required, but never shown. The list of characters can make use of character classes in the [