How to find bash shell function source code on Linux/Unix

Posted on in Categories , , last updated September 22, 2017

I am a new Linux server user. I cannot figure out how to find out the source code of a bash function named foo() which is defined. How to locate the file where a bash function is defined and how to view the source code of foo() from shell prompt itself?

bash shell view source code from bash shell and find source code defined file
To find the function definition i.e. source code fro foo() from the bash shell, run:
type -a function_name_here
type -a foo

Sample outputs:

foo is a function
foo () 
{ 
    local l="$1";
    [[ $l == "" ]] && /usr/bin/lxc exec vm -- /root/bin/uploads on;
    /usr/bin/lxc exec vm -- /bin/bash;
    [[ $l == "" ]] || return 0;
    wait;
    /usr/bin/lxc exec vm -- /root/bin/uploads off;
    sudo -i /root/bin/master.replication -v;
}

To list all function names, type:
$ declare -F
$ declare -F | grep function_name
$ declare -F | grep foo

Sample outputs:

declare -f __expand_tilde_by_ref
declare -f __get_cword_at_cursor_by_ref
declare -f __git_eread
declare -f __git_ps1
declare -f __git_ps1_colorize_gitstring
declare -f __git_ps1_show_upstream
declare -f __grub_dir
declare -f __grub_get_last_option
declare -f __grub_get_options_from_help
declare -f __grub_get_options_from_usage
declare -f __grub_list_menuentries
declare -f __grub_list_modules
declare -f __grubcomp
declare -f __ltrim_colon_completions
declare -f __parse_options
declare -f __reassemble_comp_words_by_ref
declare -f _allowed_groups
declare -f _allowed_users
declare -f _available_interfaces
declare -f _cd
....
..
...
declare -f _uids
declare -f _upvar
declare -f _upvars
declare -f _usb_ids
declare -f _user_at_host
declare -f _usergroup
declare -f _userland
declare -f _variables
declare -f _xfunc
declare -f _xinetd_services
declare -f cbz_wp_admin
declare -f command_not_found_handle
declare -f del_all_mem_cache
declare -f dequote
declare -f quote
declare -f quote_readline

To see see quote() definition:
$ declare -f quote
Sample outputs:

quote () 
{ 
    local quoted=${1//\'/\'\\\'\'};
    printf "'%s'" "$quoted"
}

How do I find the file where a bash function is defined?

The syntax is as follows:
$ shopt -s extdebug
$ declare -F quote
quote 143 /usr/share/bash-completion/bash_completion
$ declare -F cbz_wp_admin
cbz_wp_admin 14 /home/vivek/.bash_aliases

The quote() is defined /usr/share/bash-completion/bash_completion file at 143 line. Same way the cbz_wp_admin() defined in /home/vivek/.bash_aliases at line # 14. You can start it as follows too:
$ bash --debugger
$ declare -F function_name()
$ declare -F cbz_wp_admin

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin and a trainer for the Linux operating system/Unix shell scripting. He has worked with global clients and in various industries, including IT, education, defense and space research, and the nonprofit sector. Follow him on Twitter, Facebook, Google+.

1 comment

Leave a Comment