Re: Ignored alias inside for loop

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On Sun, Jul 26, 2015 at 09:23:52AM +0000, Rusty Bird wrote:
> Why does this ...

> #!/bin/sh
> for i in 1; do
>     alias foobarbaz='echo ok'
>     foobarbaz
> done

> ... result in "foobarbaz: not found", but without the for loop it works?
> Maybe I'm missing something in the spec, because bash-as-sh behaves
> the same.

Aliases are expanded during parsing, not during execution (like
functions are). The for loop is parsed completely before it is executed.

Using a function instead of an alias, or using eval will do what you
want.

Note that some parts of the parse/execute split are not specified by
POSIX. In particular, ksh93 and the real Bourne shell parse dot scripts
completely before executing anything, while most other shells parse as
needed. Example: given the file alias1.sh below:

alias foobarbaz='echo ok'
foobarbaz

The result is ok when running  dash -c '. ./alias1.sh'  but a command
not found error message when running  ksh93 -c '. ./alias1.sh'.

Similarly, a string as passed to the -c option or eval or trap builtins
may or may not be parsed fully before execution.

-- 
Jilles Tjoelker
--
To unsubscribe from this list: send the line "unsubscribe dash" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [LARTC]     [Bugtraq]     [Yosemite Forum]     [Photo]

  Powered by Linux