How do I list the functions defined in my shell?

BashShellFunctionUnix

Bash Problem Overview


I can type alias to show a list of all the aliases.

But for functions, all I can do is grep my .bash_profile.

That only gets the ones in that file, not those defined in subsidiary files or dynamically.

Is there a more convenient way to find out what functions are currently defined?

Bash Solutions


Solution 1 - Bash

declare -F

> Function names and definitions may be listed with the -f option to the > declare builtin command (see Bash Builtins). The -F option to declare > will list the function names only > (and optionally the source file and line number).

Bash Reference Manual

Solution 2 - Bash

Assuming bash shell:

typeset -f

will list the functions.

typeset -F

will list just the function names.

Solution 3 - Bash

declare -F

will give you the names of all functions

type function_name

will give you the source for a particular function

Solution 4 - Bash

declare -F actually prints declare commands and not only function names:

$ declare -F
declare -f function1
declare -f function2

You can use compgen -A function to print only function names:

$ compgen -A function
function1
function2

Solution 5 - Bash

typeset is obsolete, please use:

declare -f

or

declare -f function_name

or

type function_name

Solution 6 - Bash

set | grep " ()"

In place of grep you can also use fgrep or hgrep (hgrep is my personal favorite, it's grep but it hi-lites the 'grep-ped' result.

hgrep can be found here: ACME Labs hgrep

Solution 7 - Bash

I'm no expert at this, still learning, but after finding this question and its answer because I wanted the same, I wrote the following (based on "The Archetypal Paul's declare" answer) to give me ultimately what I was after: a formatted list of both aliases and functions:

function functionaliaslist() {
	echo
	echo -e "\033[1;4;32m""Functions:""\033[0;34m"
	declare -F | awk {'print $3'}
	echo
	echo -e "\033[1;4;32m""Aliases:""\033[0;34m"
	alias | awk {'print $2'} | awk -F= {'print $1'}
	echo
	echo -e "\033[0m"
}

That was before I saw Lri's answer, and so extrapolating from that, I replace the declare and alias lines with appropriate compgen commands instead, to get:

function functionaliaslist() {
	echo
	echo -e "\033[1;4;32m""Functions:""\033[0;34m"
	compgen -A function
	echo
	echo -e "\033[1;4;32m""Aliases:""\033[0;34m"
	compgen -A alias
	echo
	echo -e "\033[0m"
}

Woks a treat for what I wanted. Sharing in case it helps anyone else.

There are a multitude of other "actions" available to compgen -A [action] (and other options for compgen of course). I found a good write-up here which also includes a link to the man page (because man compgen doesn't work in some cases).

Solution 8 - Bash

Bash shell script specific answer

(because this question was marked as a duplicate of this one)

If you have functions exported in your shell a called script will see them. But luckily, they get marked as such.

Copy-Pasta Demo script

#!/bin/bash
foo(){ :;}; bar(){ :;}; baz(){ :;}; export -f foo bar

export -pf

bash -c $'spam(){ :;}; \
echo "RAW:"; \
declare -pF; \
echo "FILTERED:"; \
declare -pF | awk \'$2 !~ /x/{print$3}\''

Transcript in Bash

/tmp $ foo(){ :;}; bar(){ :;}; baz(){ :;}; export -f foo bar

/tmp $ export -pf
bar ()
{
    :
}
declare -fx bar
foo ()
{
    :
}
declare -fx foo

/tmp $ bash -c $'spam(){ :;}; echo "RAW:"; declare -pF; echo "FILTERED:"; declare -pF | awk \'$2 !~ /x/{print$3}\''
RAW:
declare -fx bar
declare -fx foo
declare -f spam
FILTERED:
spam

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionDovView Question on Stackoverflow
Solution 1 - BashThe Archetypal PaulView Answer on Stackoverflow
Solution 2 - BashRandall BohnView Answer on Stackoverflow
Solution 3 - BashJohn Lawrence AspdenView Answer on Stackoverflow
Solution 4 - BashLriView Answer on Stackoverflow
Solution 5 - BashRobertView Answer on Stackoverflow
Solution 6 - BashAtomicKoboldView Answer on Stackoverflow
Solution 7 - BashDavidTView Answer on Stackoverflow
Solution 8 - BashBruno BronoskyView Answer on Stackoverflow