Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Stripping spaces from a shell variable, portably

    Hi Bart :)

 * Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> dixit:
> }     I need to strip leading and trailing spaces from the contents of
> } a shell variable, and I need to do it [...] using SuSv3
> } constructs, not Zsh extensions.
> What about:
> 	tmp="${variable##*[^ ]}"
> 	variable="${variable%${tmp}}"
> 	tmp="${variable%%[^ ]*}"
> 	variable="${variable#${tmp}}"

    You're an effin genius, Bart ;)) I didn't even thing about using
a temporary... I wrote a similar solution, but using Zsh nested
substitution feature (getting rid of the temporary) in two
assignments, one for stripping the leading, another for the trailing.

    Thanks a lot :)) This is for a shell based building system, just
like autoconf but without autodetection (by now) and easier for
developers (they don't need to learn almost anything), that will be
published, if possible, this month. It's working since some time, but
now I'm doing some cleanup, and I needed to sanitize the options and
other things in the code. Most of the trimming is just one space, but
in a few points there are more... Thanks for your help :)

    If everybody in the world used zsh, I would have had no problems
writing this building system... but unfortunately I must write it
portably :( At least is GPL'd ;)

    Raúl Núñez de Arenas Coronado

Linux Registered User 88736
http://www.pleyades.net & http://raul.pleyades.net/

Messages sorted by: Reverse Date, Date, Thread, Author