|
发表于 2006-2-22 12:05:42
|
显示全部楼层
这个我还没有想过,不过试了一下,果如你说。别名的处理要在检查keyword之前。google看了一下关于POSIX标准的命令执行顺序,alias是第一位的,所以我想可能home_king兄的2,3要调一调的吧。这里附上POSIX command execution sequence:
An overview of the POSIX command execution sequence, which
covers (in general order of processing):
1) aliases
2) reserved keywords (what look like programming constructs):
! { } case do done elif else esac fi for if in then until while
3) special built-ins:
break : continue . eval exec exit export readonly
return set shift times trap unset
4) functions
5) regular built-ins:
alias bg cd command false fc fg getopts jobs kill newgrp
pwd read true umask unalias wait
Aliases
-------
- aliases can be defined to override anything from special
built-ins on down. Technically, they can be *defined*
to override reserved keywords, but this substitution will
not be allowed at processing time (unlike standard bash
behavior), so you might as well just say you can't redefine
keywords.
- to override normal alias processing, put the alias in quotes,
or backslash escape it:
$ set runs "set"
$ alias set=ls
$ set runs "ls"
$ "set" runs "set"
$ \set also runs "set"
Special versus regular built-ins
------- ------ ------- ---------
The differences between special and regular built-ins:
1) syntax errors in special built-ins *may* cause a shell to abort,
while errors in a regular built-in shall not
2) variable assignments specified with specials shall remain
in effect after the special completes, but not so with
regulars.
So you have the difference between, say
$ VAR=value set (VAR available further on)
$ VAR=value jobs (VAR *not* available further on)
And the other big difference is, clearly, that you can define
a function to override a regular, but not a special, built-in.
Functions
---------
POSIX standard:
f () {
....
}
"When a function is executed, it shall have the syntax-error
and variable assignment properties described for special
built-in utilities ...". Yuck.
The "command" command
--- --------- -------
Putting the "command" command in front of a command does
a number of things:
1) if the command is an alias, it prevents alias expansion
2)
"If the command_name is the same as the name of one of the
special built-ins, the special properties in the enumerated
list [above] shall not occur. In every other respect, if
command_name is not the name of a function, the effect of
"command" (with no options) shall be the same as omitting
"command".
3) And, as you see above, it suppresses function lookup,
so you can override *regular* shell builtins with functions
of the same name, so you could override the built-in cd
command with a function of the same name:
cd () {
.....
command cd ...
.....
}
附上关于builtin的注释:
The different categories of "built-in" commands:
reserved keywords:
! { } case do done elif else esca fi for if in then until while
special builtins:
break : continue . eval exec exit export readonly
return set shift times trap unset
regular builtins:
alias bg cd command false fc fg getopts jobs kill newgrp
pwd read true umask unalias wait |
|