Subject: bin/3145: Four problems of sh
To: None <gnats-bugs@gnats.netbsd.org>
From: None <hiroy@netcom.com>
List: netbsd-bugs
Date: 01/26/1997 10:27:58
>Number:         3145
>Category:       bin
>Synopsis:       four strange parameter expansions in sh
>Confidential:   no
>Severity:       serious
>Priority:       high
>Responsible:    bin-bug-people (Utility Bug People)
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Jan 26 10:35:00 1997
>Last-Modified:
>Originator:     Hiroyuki Ito
>Organization:
	None
>Release:        NetBSD-current 1997/01/18
>Environment:
System: NetBSD 1.2B i386

>Description:
	strangeness of
	1. ${10}
	2. ${parameter#${parameter}}
	3. ${1:-word} when $1 is null
	4. $((${#parameter}+1))

>How-To-Repeat:

% sh
$ r () { man sh | col -b | sed $1!d | cat -s; }
$ #1
$ echo ${10}
Syntax error: Bad substitution
$ r 981,1002
       The simplest form for parameter expansion is:

	   ${parameter}

							       15

SH(1)							    SH(1)

       The value, if any, of parameter is substituted.

       The parameter name or symbol can be enclosed in braces,
       which are optional except for positional parameters with
       more than one digit or when parameter is followed by a
       character that could be interpreted as part of the name.
$ #2
$ a=abcd; echo XX${a#$a}YY XX${a#abcd}YY
XXabcd XXabcd
$ r 1087,1092
       ${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 pat-
	      tern deleted.
$ #3
$ set -- ""; a=$1; echo "$# '$1' '${1:-wwww}' '${a:-wwww}'"
1 '' '' 'wwww'
$ r 1015,1018
       ${parameter:-word}
	      Use Default Values.  If parameter is unset or null,
	      the expansion of word is substituted; otherwise,
	      the value of parameter is substituted.
$ #4
$ hd () hexdump -e '"%06_ax " 8/1 " %02x"' -e '"  " "%_p"' -e '"\n"' "$@"
$ a=xy; echo -n "$((${#a}+1))" | hd
000000  33 20 2b 31 90 a7 04     3 +1...
$ r 1047,1049
       ${#parameter}
	      String Length.  The length in characters of the
	      value of parameter.
$ r 1143,1156
       Arithmetic expansion provides a mechanism for evaluating
       an arithmetic expression and substituting its value. The
       format for arithmetic expansion is as follows:

	      $((expression))

       The expression is treated as if it were in double-quotes,
       except that a double-quote inside the expression is not
       treated specially.  The shell expands all tokens in the
       expression for parameter expansion, command substitution,
       and quote removal.

       Next, the shell treats this as an arithmetic expression
       and substitutes the value of the expression.
$ 

>Fix:
	Thank you.
>Audit-Trail:
>Unformatted: