[gnu.bash.bug] for without in

trost@reed.bitnet (Bill Trost) (12/14/89)

I've noticed that something like 

	spam() { for i; do echo $i; done }

expands into

	spam () { for i in $*
	do
	echo $i
	done }

(direct shell output).  Personally, I would expect "$@" instead of $*
in the for statement.  What is the "official statement" on this?

bfox@sbphy.ai.mit.edu (Brian Fox) (12/14/89)

   Date: 14 Dec 89 05:10:31 GMT
   From: ogicse!reed!trost@caesar.cs.montana.edu  (Bill Trost)
   Sender: bug-bash-request@prep.ai.mit.edu

   I've noticed that something like 

	   spam() { for i; do echo $i; done }

   expands into

	   spam () { for i in $*
	   do
	   echo $i
	   done }

   (direct shell output).  Personally, I would expect "$@" instead of $*
   in the for statement.  What is the "official statement" on this?

You are right.  The shell should expand the null argument to "$@".

Brian

trost@reed.bitnet (Bill Trost) (12/15/89)

OK, *now* I write:

	spam () { for i in "$@"; do echo $i; done }

and if I run

	spam "a b" c

I get
	a b c

Similarly, if I leave out the double-quotes, then

	spam "a b" c

produces
	a
	b
	c

It looks like the arguments to functions aren't getting passed quite
right.  This looks familiar; is it one of Chet's
I-have-fixed-this-and-sent-the-patch-to-Brian things?

This series of problems was generated by my attempts to fix the
problem Anders Ellefsrud <anders@ifi.uio.no> noted when he posted a
*corrected* autoload function.  (Silly me, I only implemented the
load, I didn't have it *do* anything!)

BTW, the current candidate for autoload is:
	autoload() {
	   eval $1 '() { args=$@ ; . '$2' ; set -- $args ;  return '$1' "$@" }'
	}

It really should check to see that it has two arguments; I got really
confused when I kept forgetting to provide the filename to
autoload....  Also should check $2 for a leading slash and prepend
$PWD/ if it's absent.

You need the return in case the autoloaded function returns something;
the "$@" is what this message is about; it *should* be right, although
it doesn't work as expected (hence this posting).

Bryan, you asked something about "what is autoload", essentially.  I
think we're probably all basing our discussion on what we've picked up
from emacs' autoload.  I'll spare you the agony of reading the output
of C-h f autoload   :-).

chet@cwns1.CWRU.EDU (Chet Ramey) (12/16/89)

In article <13746@reed.UUCP> trost@reed.bitnet (Bill Trost) writes:

	[about "$@" not working right in shell functions]

>It looks like the arguments to functions aren't getting passed quite
>right.  This looks familiar; is it one of Chet's
>I-have-fixed-this-and-sent-the-patch-to-Brian things?

You got it.

It's not the arguments to the function, it's the problem with "$@" 
being interpreted the same as $@ (there is no special-casing for the
quotes in vanilla bash-1.04).

>BTW, the current candidate for autoload is:
>	autoload() {
>	   eval $1 '() { args=$@ ; . '$2' ; set -- $args ;  return '$1' "$@" }'
>	}

Check out <1989Dec14.212828.5302@usenet.ins.cwru.edu> (love them C-News
message-id strings!).

It is, I think (in my own highly opinionated fashion), the closest thing to
the ksh `autoload' that can be implemented as a shell function.  Bill is
right about the need for a `return' though (even if he did it wrong :-);
I have put that into mine.

Chet Ramey
-- 
Chet Ramey
Network Services Group				"Where's my froggie?"
Case Western Reserve University
chet@ins.CWRU.Edu