26

I have setup several functions in my .bashrc file. I would like to just display the actual code of the function and not execute it, to quickly refer to something.

Is there any way, we could see the function definition?

erch
  • 5,030
mtk
  • 27,530
  • 35
  • 94
  • 130

4 Answers4

29

The declare builtin's -f option does that:

bash-4.2$ declare -f apropos1
apropos1 () 
{ 
    apropos "$@" | grep ' (1.*) '
}

I use type for that purpose, it is shorter to type ;)

bash-4.2$ type apropos1
apropos1 is a function
apropos1 () 
{ 
    apropos "$@" | grep ' (1.*) '
}
manatwork
  • 31,277
7

You can use the type command to do this.

type yourfunc will print the function to STDOUT. As man type says,

The type utility shall indicate how each argument would be interpreted if used as a command name.
jasonwryan
  • 73,126
0

for builtin commands' info use:

help [-s|-d] COMMAND1 COMMAND2 ....

for example:

help help alias

For info about all of them type, for example:

help -s '' 
Robert
  • 1
-1

type works if you declared your function in the shell but which works even if you sourced the function from another file.

Tom
  • 1
  • That's not true. type works fine no matter where you've sourced the function from. Try printf 'foo(){ echo "hi"; }\n' > file; . file; type foo. Actually, which doesn't work with functions at all, it only finds commands in your PATH. See Why not use "which"? What to use then? – terdon Nov 02 '22 at 09:54
  • @terdon, depends on the implementation of which, GNU which can be configured to dump function definitions (as you'll see at the Q&A you're linking to) – Stéphane Chazelas Nov 02 '22 at 10:27
  • @StéphaneChazelas even there, you would need something like declare -f | which --read-functions --tty-only $functionName and it still makes no difference whether the function was defined in the current shell or sourced from a file. – terdon Nov 02 '22 at 10:40