Skip to content

Zeroplex 生活隨筆

軟體開發、伺服器和生活瑣事

小 縮小字型大小。 中 重設字型大小。 大 放大字型大小。

type (in Bash) 來判別 shell 實際執行的命令

Posted on 2018 年 8 月 27 日2021 年 3 月 12 日 By 日落 在〈type (in Bash) 來判別 shell 實際執行的命令〉中尚無留言

Linux 的 shell 提供了很多彈性,讓使用者可以客製化自己的工作環境。但也因此有時候把環境搞亂了自己也沒發現。

例如說:

johnroyer@box:~$ phpunit --version
PHPUnit 7.3.2 by Sebastian Bergmann and contributors.

johnroyer@box:~$ cd devel/laravel/
johnroyer@box:~/devel/laravel$ phpunit --version
PHPUnit 6.5.12 by Sebastian Bergmann and contributors.

這個時候雖然可以使用 which 來找出實際上執行的指令是哪一個 binary,但有時不一定與執行的是同一個。例如,上面的環境中,執行 which 都會有相同的結果:

$ which phpunit
/home/johnroyer/.config/composer/vendor/bin/phpunit

如果使用 type (Bash built-in) 來檢查的話,則會更清楚的告訴你,指令會如何執行,例如:

$ type phpunit
phpunit is a function
phpunit () 
{ 
    REPO_PHPUNIT=`pwd`"/vendor/bin/phpunit";
    if [ -e $REPO_PHPUNIT ]; then
        $REPO_PHPUNIT $*;
    else
        /home/johnroyer/.config/composer/vendor/bin/phpunit $*;
    fi
}

which 是由 $PATH 環境變數中的路徑來找出對應的執行擋路徑;type 則是由 Bash 當下的環境去檢查當下到底會如何執行。

Tags:Bash, Linux

文章導覽

Previous Post: 氣喘藥可能會使心跳加速
Next Post: PHP SplPriorityQueue …. WTF!

發佈留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *


其他

關於我  (About me)

小額贊助

  文章 RSS Feed

  留言 RSS Feed

Apache AWS Bash C/C++ Docker FreeBSD GCP Git Google Java JavaScript Laravel Linux Microsoft MSSQL MySQL Nginx PHP PHPUnit PostgreSQL Python Qt Ubuntu Unix Vim Web Windows WordPress XD 作業系統 分享 好站推薦 專題 攝影 新奇搞笑 新聞 旅遊 生活雜記 程式設計 網路架站 網頁設計 資訊學習 資訊安全 遊戲 音樂


創用 CC 授權條款
本著作係採用創用 CC 姓名標示-相同方式分享 4.0 國際 授權條款授權.