Bash shell 的 Function 查看 與 撰寫

Ubuntu / Debian Linux 預設都是使用 Bash 當 shell, 此篇主要紀錄 Function 的寫法 和 如何查看目前有哪些 Function 等等.

下述範例參考自此文: Understanding Functions

Bash shell function 的寫法

Bash shell function 的寫法有下述幾種:

  1. function () { command ; command; }
  2. function function_name { command ; command; }
  3. function function_name () { command ; command; }
  4. 範例: dfh() { df -h; }

Bash shell function 有哪些 (Function List 清單)

  • declare -F # 列出有目前哪些 function
  • declare -f # 列出有目前哪些 function, function 詳細內容
  • declare -f start_agent # 列出 start_agent 這個 function 的內容
  • unset -f function_name # 清除此 Function

zsh 的話, declare -f 可以用, 剩下得要自行使用 declare -h 自行嘗試囉~ 🙂

作者: Tsung

對新奇的事物都很有興趣, 喜歡簡單的東西, 過簡單的生活.

發表迴響

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料