Shell 命令执行顺序分析[图]

2019-09-23 09:50:47刘景俊

token 4.先前的 token 4则成了 token 5 与 token 6。结果:

echo /tmp/x/${f}[12] a b cmd subst 5
| 1 | |------- 2 -------| 3 4 |-5-| |- 6 -| 7

9.最后的替换阶段是通配符展开。token 2 变成了 token 2 与 token 3:

echo /tmp/x/$f1 /tmp/x/$f2 a b cmd subst 5
| 1 | |---- 2 ----| |---- 3 ----| 4 5 |-6-| |- 7 -| 8

10.这时,Shell已经准备好了要执行最后的命令了。它会去寻找 echo。正好 ksh93 与 bash 的 echo 都内建到Shell 中了。

11.Shell实际执行命令。首先执行 > out 的 I/O重定向,再调用内部的 echo 版本,显示最后的参数。

最后的结果:

$cat out
/tmp/x/f1 /tmp/x/f2 a b cmd subst 5