尋找磁碟上 shell/bash 函數的位置

尋找磁碟上 shell/bash 函數的位置

如果我有這樣的函數:

    #!/usr/bin/env bash

    function foo {

    }

上面的函數位於 .bash_profile 或其他地方。

如果我使用which foo它,它不會給我該命令在 shell 腳本中的位置。

有什麼方法可以讓我發現它的位置或是grep我最好的選擇嗎?

答案1

這個問題已經有答案了這裡。因此,從這個事實可以得出兩件事:

  1. 在在這裡發布您的問題之前,您應該進行更好的研究(但是,我們有時都會很懶)
  2. 有一個更好的技巧可以grep做你想做的事

因此,您可以在 PATH 中取得原始檔案和 shell 函數的行號,如下所示:(信用:這個答案

# Turn on extended shell debugging
shopt -s extdebug

# Dump the function's name, line number and fully qualified source file  
declare -F foo

# Turn off extended shell debugging
shopt -u extdebug

答案2

which只搜尋路徑。若要查看 bash 實際執行的內容,請使用內建指令type

type foo

相關內容