Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 11 Jun 1999 13:08:31 -0400 (EDT)
From:      Mikhail Teterin <mi@misha.cisco.com>
To:        FreeBSD-gnats-submit@freebsd.org
Subject:   bin/12137: something wrong with shell -- functions with arithm expressions
Message-ID:  <199906111708.NAA57393@misha.cisco.com>

next in thread | raw e-mail | index | archive | help

>Number:         12137
>Category:       bin
>Synopsis:       something wrong with shell -- functions with arithm expressions
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    freebsd-bugs
>State:          open
>Quarter:        
>Keywords:       
>Date-Required:
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Fri Jun 11 10:10:01 PDT 1999
>Closed-Date:
>Last-Modified:
>Originator:     Mikhail Teterin
>Release:        FreeBSD 3.2-STABLE i386
>Organization:
Virtual Estates, Inc.
>Environment:

>Description:

	This strange method of figuring out  the length of a string gives
	different results for the strings of the same length depending on
	their content:

		length () {
			echo $((0 + ${#1}))
		}

		length "abc (def [xyz])"
		length "abc (def (xyz))"

	The use of ``[]'' makes the string appear one character longer. I
	made  the severity  and priority  fairly high,  because this  may
	indicate some brokenness in more important parts of sh.

	A more simple attempt to reproduce this:

		a="abc (def [xyz])"
		echo $((0+${#a}))
		b="abc (def (xyz))"
		echo $((0+${#b}))

	works as expected -- same length for both strings. So does:

		length () {
			echo ${#1}
		}

		length "abc (def [xyz])"
		length "abc (def (xyz))"

	You actually need the combination of arithmetic and function, it seems.

>How-To-Repeat:

	Just run the script above with sh. I see:
	16
	15

>Fix:

	Use bash or ksh -- they both produce 15 for both of this lines.

>Release-Note:
>Audit-Trail:
>Unformatted:


To Unsubscribe: send mail to majordomo@FreeBSD.org
with "unsubscribe freebsd-bugs" in the body of the message




Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?199906111708.NAA57393>