当通过符号链接访问 bash 脚本时,我很难理解 bash 脚本如何处理参数的微妙之处。这个问题最容易用命令及其输出来解释:
$ type ll
ll is aliased to `ls -Alt --color=auto'
$ cat myshellscript
#!/bin/bash
type $1
$ ls -l myshellscript
-rwxr-xr-x 1 pi pi 20 Aug 22 19:55 myshellscript
$ . myshellscript ll
ll is aliased to `ls -Alt --color=auto'
$ ls -l /usr/local/bin
total 0
lrwxrwxrwx 1 root root 21 Aug 22 19:56 mss -> /home/pi/sh/myshellscript
$ mss ls
ls is /bin/ls
$ echo "OK, that worked"
OK, that worked
$ mss ll
/usr/local/bin/st: line 2: type: ll: not found
$ echo "Why didn't that work?"
Why didn't that work?
答案1
默认情况下,脚本内禁用别名扩展:参见示例
当你执行
. myshellscript ll
你不只是在跑步myshellscript
,你是采购它进入当前(交互式)shell,其中别名是扩大了。相反,当你跑步时
mss ls
您只是运行脚本,并且不会发生别名扩展 - 正如预期的那样。如果你运行./myshellscript ll
而不是运行,你会看到相同的结果. myshellscript ll