Shell-Script

無法“看到”別名的 bash 腳本的符號連結

  • August 24, 2019

當通過符號連結訪問 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?

預設情況下,在腳本中禁用別名擴展:參見例如

當你執行

. myshellscript ll

您不只是在執行myshellscript,而是將其採購到目前(互動式)shell 中,其中擴展了別名。相反,當你執行

mss ls

您只是在執行腳本,並且不會發生別名擴展 - 正如預期的那樣。如果您執行./myshellscript ll而不是,您會看到相同的結果. myshellscript ll

引用自:https://unix.stackexchange.com/questions/537149