System V.2 sh functions and parameter passing

David Elliott dce at hammer.UUCP
Fri Jul 19 02:58:13 AEST 1985


The documentation on functions in the System V.2 Bourne Shell is
very sparse and is very vague on the point of parameter passing.

The only documentation I could find was in the sh(1) manual page,
and this says something like: when a function is executed, the positional
parameters ($1, $2, ...) are set to the function arguments. This doesn't
completely specify what really happens, since it doesn't actually
define when a function is known to be a function.

Take the following example:

	#!/bin/sh
	foo()
	{
		echo "$@"
	}

	echo "$@"
	foo a b c
	echo "$@"

When executed with the arguments "x y z", the results are

	x y z
	a b c
	a b c

This would imply that running "foo a b c" is the same as running the commands
"set a b c; foo".

When the line "foo a b c" is changed to "( foo a b c )", to run the
function in a subshell, the results are

	x y z
	a b c
	x y z

Which strengthens the assumption that the documentation means that the
positional parameters are being set by the function execution and left.

My questions:

	1. Has anybody at AT&T worked on a method of parameter stacking
	   or changing the shell so that the original parameter values
	   are saved? (No, parameters can not be saved and restored by
	   using code like:

		ARGS="$*"
		...
		set $ARGS
	   
	   for obvious reasons, and it may not be possible to parse the
	   entire argument list before a function execution is needed.)

	2. Could someone at AT&T fully define the behavior of functions
	   and put this in the documentation, or tell us where the
	   behavior is defined? I'd hate to depend on the way functions
	   currently work and then find a change made later.

			David Elliott
			{decvax|ihnp4}!tektronix!tekecs!dce



More information about the Net.bugs.usg mailing list