368

Is there a way to view a bash function's definition in bash?

For example, say I defined the function foobar

function foobar {
    echo "I'm foobar"
}

Is there any way to later get the code that foobar runs?

$ # non-working pseudocode
$ echo $foobar
echo "I'm foobar"

4 Answers 4

480

Use type. If foobar is e.g. defined in your ~/.profile:

$ type foobar
foobar is a function
foobar {
    echo "I'm foobar"
}

This does find out what foobar was, and if it was defined as a function it calls declare -f as explained by pmohandras.

To print out just the body of the function (i.e. the code) use sed:

type foobar | sed '1,3d;$d'
4
  • 2
    @sjsupersumit The question explicitly asks for a Bash solution.
    – bfontaine
    Commented Sep 5, 2018 at 8:59
  • 4
    type will also expand aliases, which is a nice bonus :) Commented Jun 29, 2020 at 1:37
  • 4
    this doesn't work for me. It doesn't show the definition of the function, it only shows which file it came from.
    – gman
    Commented Sep 2, 2021 at 20:40
  • 5
    @gman I had the same issue. You are probably using zsh. In that case, declare -f function_name will work on both bash and zsh
    – Aserre
    Commented Apr 13, 2022 at 8:53
327

You can display the definition of a function in bash using declare. For example:

declare -f foobar
6
  • 8
    Great, to the point, works even when functions defined in other loaded script files. Works also in zsh. Commented Oct 26, 2017 at 13:44
  • 3
    Works with old shells on non-linux systems if you use typeset -f
    – Emmanuel
    Commented Dec 2, 2017 at 19:38
  • good also that it just shows the function definition, so you don't have to parse if that's what you want.
    – ribamar
    Commented Mar 7, 2018 at 10:04
  • 1
    The best answer is on the bottom. It doesn't make sense.
    – Bijan
    Commented Aug 11, 2020 at 4:56
  • 1
    Works also in zsh.
    – TNT
    Commented Oct 13, 2020 at 12:22
8
set | sed -n '/^foobar ()/,/^}/p'

This basically prints the lines from your set command starting with the function name foobar () and ending with }

1
  • 3
    I'm positively surprised that the set output has a) comments stripped out, and b) normalized the white space between function name and parens. I'm still reluctant to use this as there may be variables that contain } that may trip the simple parsing.
    – Robert
    Commented Jan 5, 2021 at 19:12
4
set | grep -A999 '^foobar ()' | grep -m1 -B999 '^}'

with foobar being the function name.

2
  • 5
    problem: will only display up to the first "}", which is not everything whenever the defintion contains nestings of "{...}" which indeed Bash does allow. Commented Jul 21, 2014 at 2:45
  • 2
    Can also fail if the function contains a here-doc/here-string containing the curly-brace pattern
    – Cheetah
    Commented Sep 28, 2015 at 16:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.