sh functions with "local variables"

David Elliott dce at Solbourne.COM
Wed Jun 7 13:10:48 AEST 1989


Here's an interesting problem for all of you sh/ksh wizards out
there.

I normally write shell scripts using a template like this:

	main()
	{
		...
	}
	sub1()
	{
		...
	}
	...
	main ${1+"$@"}	# don't ask, just use it

Now, in modern versions of sh (post SVR2, I think), function parameters
are local to the function, but there's no way to have local variables
otherwise.

Let me clarify this a little.  Yes, if you force a subshell, either
by parenthese or redirection, the changes in the subshell don't affect
the main thread, but that's not really what I'm looking for.

Now, if the function doesn't have any arguments, or if you can use
the arguments early and get rid of them, you can use "set" to put
values in $1-$9, which is one way to have local variables.  Still,
real local variables would be great.

Of course, arrays would be nice, too.

Maybe it's time to implement sh++?

-- 
David Elliott		dce at Solbourne.COM
			...!{boulder,nbires,sun}!stan!dce



More information about the Comp.unix.wizards mailing list