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

Re: Stripping spaces from a shell variable, portably



    Hi Thomas :)

 * Thomas Köhler <jean-luc@xxxxxxxxxxxxxxxxx> dixit:
> Why not
>   variable=`echo "$variable" | sed -e 's/^ *//' -e 's/ *$//'`

    Because if possible I want to do it just with shell code, without
external programs. In fact, instead of sed I would prefer do with
'tr', so the code is not dependant on sed. 'tr' won't do a good job
if the variable contents have spaces (legal ones) interspersed, so if
sed is the only solution I would prefer to have the nasty spaces
instead.

> I'm not sure whether all versions of sed understand multiple
> "-e" arguments, so it might me necessary to use this:

    According to SuSv3 (since the shell script will be SuSv3
compliant, I'd better use it for sed, too), you can provide multiple
-e options, so if I use 'sed' (not probable, as I tell in the last
paragraph) I will use multiple -e arguments.

    Thanks anyway, Thomas. Really these kind of processes should be
done by sed, not shell code, if you want portability, but in this
particular case I prefer the loop: slower, nastier, but not dependent
on sed (although sed should be present on every Unix system out
there, specially SuSv3 compliant ones...).

    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