How to print a function definition in Bash?

Bash

Bash Problem Overview


I have defined a few different functions in my .bash_profile. I usually remember the name of function but want to take a quick peek in the code before I run it.

In my .bash_profile I have the following:

gpm () {
  echo "git pull origin master"
  git pull origin master
}

Now I want to run something like this in Bash:

$ <something> gpm

Result expected: Don't execute the function just print out the function definition itself.

Bash Solutions


Solution 1 - Bash

EDIT: The best answer isn't this one, but the other one below.

What this answer used to say is that you can get a function definition in bash using the type builtin, e.g. type gpm. However, using declare as described in the other answer is better in every way.

Solution 2 - Bash

declare -f gpm will just print the function definition of function gpm with no other text.

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
QuestionOmar JalalzadaView Question on Stackoverflow
Solution 1 - BashsorpigalView Answer on Stackoverflow
Solution 2 - Bashglenn jackmanView Answer on Stackoverflow