Here's the substr function which does what you want:
##########################
# Usage: substr string [offset [length]]
# offset values start from 0 (that is, first character has offset 0)
substr()
{
typeset _str="$1"
typeset -i _strlen=${#_str}
typeset -i _offset="${2:-0}"
typeset -i _sublen="${3}"
typeset -i _rlen
((_rlen=_strlen-_offset))
typeset -R${_rlen} _substr=${_str}
typeset -L${_sublen} _substr=${_substr}
echo $_substr
}
##########################
$ substr 1234567890 3 4
4567
$ substr 1234567890 3
4567890
$ substr 1234567890 0 10
1234567890
One caveat, though: It doesn't work if your string contains leading or trailing blanks.
Main Topics
Browse All Topics





by: ozoPosted on 1999-02-18 at 08:20:16ID: 1296656
STRING=12345678
STRING=${STRING#???}
STRING=${STRING%??}
echo $STRING