On Wed, 4 Nov 2009, Eric Blake wrote:
> According to Greg Wooledge on 11/4/2009 6:23 AM:
> > On the other hand, removing a single leading zero is not difficult:
> >
> > month=$(date +%m) month=${month#0} # Removing leading 0
>
> Not portable. Assigning the same variable twice in the same statement has
> different order of operations in some shells. Use:
>
> month=$(date +%m); month=${month#0}
>
> instead.
>
> > next_month=$(( ($month == 12) ? 1 : $month+1 ))
> >
> > Removing multiple leading zeroes, however, requires either a loop, or the
> > use of extended globs.
>
> Not true. You can do it via POSIX and without a loop by using an
> intermediate variable:
>
> foo=00081
> bar=${foo%%[!0]*}
> foo=${foo#$bar}}
Or even without an intermediate variable:
foo=${foo#${foo%%[!0]*}}
(Though I prefer the variable for legibility.)
--
Chris F.A. Johnson, webmaster <http://woodbine-gerrard.com>
===================================================================
Author:
Shell Scripting Recipes: A Problem-Solution Approach (2005, Apress)
Pro Bash Programming: Scripting the GNU/Linux Shell (2009, Apress)