307

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?

codeforester
  • 39,467
  • 16
  • 112
  • 140
Dov
  • 8,000
  • 8
  • 46
  • 75
  • 4
    @AlastairIrvine Thanks for the duplicate nomination; however, I resolved it the other way around. This question has significantly more views and upvotes, as well as a broader selection of answers; and the accepted answer on your nominated duplicate is decidedly obscure and arguably just wrong. – tripleee Oct 23 '18 at 04:25

8 Answers8

369

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

Nick T
  • 25,754
  • 12
  • 83
  • 121
The Archetypal Paul
  • 41,321
  • 20
  • 104
  • 134
  • 4
    in GNU bash 4.3.46(1)-release (x86_64-pc-linux-gnu) -F lists names / -f lists whole function – ceph3us Jul 29 '16 at 05:35
  • 5
    To get the "optional" source file/line#, it appears you must enable extdebug (`shopt -s extdebug`) AND supply one or more function names to the `declare -F` command. IOW, `declare -F` does **not** supply filenames/line#s even when extdebug is enabled, which is not clear from the documentation, nor is it particularly intuitive, IMO. – Ron Burk May 16 '17 at 08:35
  • See @user495470's answer below for a better solution. – starbeamrainbowlabs Nov 19 '18 at 11:02
  • In `zsh` (Mac) this was helpful: `$ declare -f > functions.sh` – Vladimir Vukanac Nov 17 '21 at 12:43
  • Doesn't work on Mac. Prints `EPOCHREALTIME`. `compgen -A function` from @DavidT and @Lri answers works – callOfCode Jan 11 '23 at 14:13
103

Assuming bash shell:

typeset -f

will list the functions.

typeset -F

will list just the function names.

Randall Bohn
  • 2,597
  • 2
  • 16
  • 20
80
declare -F

will give you the names of all functions

type function_name

will give you the source for a particular function

John Lawrence Aspden
  • 17,124
  • 11
  • 67
  • 110
75

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
Lri
  • 26,768
  • 8
  • 84
  • 82
38

typeset is obsolete, please use:

declare -f

or

declare -f function_name

or

type function_name
Robert
  • 381
  • 3
  • 2
7
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

randomusername
  • 7,927
  • 23
  • 50
AtomicKobold
  • 99
  • 1
  • 1
  • 1
    This will list functions but also variables that have ` ()` in value – Alois Mahdal Mar 25 '16 at 10:01
  • 2
    @AloisMahdal Here's one that works: `set | grep " () $" | cut -d' ' -f1`. But it's overly complicated compared to the other answers. – wjandrea Jul 09 '16 at 01:34
  • 5
    `grep` is not a good solution for this, strings that match function definitions will cause false positives. @wjandrea can still be caught by multi line strings, as will any regex. – Matt Apr 07 '18 at 21:56
7

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).

HoldOffHunger
  • 18,769
  • 10
  • 104
  • 133
DavidT
  • 655
  • 7
  • 19
1

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
Bruno Bronosky
  • 66,273
  • 12
  • 162
  • 149