check variable content size in sh script

Polytropon freebsd at edvax.de
Sat May 18 16:06:32 UTC 2013


On Sat, 18 May 2013 11:58:30 -0400, Quartz wrote:
> 
> >> newfoo=${foo:0:51}
> >>
> >
> > That works for bash, not sh.
> 
> Ok granted, but I don't think that ${#foo} is straight sh either, so I 
> assumed "things bash/tcsh/ksh/whatever accept when running in sh 
> emulation" were ok.

By default, there is no bash on FreeBSD, and therefor no emulation
and "implicit features". :-)

At least FreeBSD's implementation of sh (which is ash, I think)
supports the # functionality. From "man sh":

     ${#parameter}
             String Length.  The length in characters of the value of
             parameter.

And:

     ${parameter#word}
             Remove Smallest Prefix Pattern.  The word is expanded to produce
             a pattern.  The parameter expansion then results in parameter,
             with the smallest portion of the prefix matched by the pattern
             deleted.

Check the chapter "Parameter Expansion" for more surprising
things that are supported by ye olde /bin/sh. :-)



-- 
Polytropon
Magdeburg, Germany
Happy FreeBSD user since 4.0
Andra moi ennepe, Mousa, ...


More information about the freebsd-questions mailing list