Bash Functions in Scope

Bash 4 is a lovely collection of globally shared state and scope. Variables and functions are declared, defined, and often redefined in many places and in a hopefully predictable order.

Here is my goto list of commands to help narrow down the field when I'm trying to debug things,

  • which is a quick way to find out where a command resides -- if it does at all
    • Using the -a option, you can see all of the places where the command could be defined
    • By default, functions and aliases are not searched by which. However, the manual documentation for which suggests you set which to the following, 
    • (alias; declare -f) | /usr/bin/which --tty-only --read-alias --read-functions --show-tilde --show-dot $@
  • type is a big step above which and will tell you more in depth information about the command
    • Using the -a option, you can see all of the places where the command could be defined
  • env with no arguments, this lists the global variables in scope
  • declare on its own will list every variable and function defined. It can give you a little more information about functions than the methods above.
    • Use the -f option to see the definition of the function
    • Use the -F option to show only the name and attributes.
    • To see where a function is defined (file and line number), you have to enable the extdebug shell option. This is probably my favorite!
      • shopt -s extdebug && declare -F $yourFunctionName

Comments

Recent posts