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
🐧 1 comment so far... add one ↓
Category | List of Unix and Linux commands |
---|---|
File Management | cat |
Firewall | Alpine Awall • CentOS 8 • OpenSUSE • RHEL 8 • Ubuntu 16.04 • Ubuntu 18.04 • Ubuntu 20.04 |
Network Utilities | dig • host • ip • nmap |
OpenVPN | CentOS 7 • CentOS 8 • Debian 10 • Debian 8/9 • Ubuntu 18.04 • Ubuntu 20.04 |
Package Manager | apk • apt |
Processes Management | bg • chroot • cron • disown • fg • jobs • killall • kill • pidof • pstree • pwdx • time |
Searching | grep • whereis • which |
User Information | groups • id • lastcomm • last • lid/libuser-lid • logname • members • users • whoami • who • w |
WireGuard VPN | Alpine • CentOS 8 • Debian 10 • Firewall • Ubuntu 20.04 |
type functionname will also display function