12

Problem:

I have multiple bash functions and aliases. I can't remember all of them off the top of my head, so I usually end up opening my .bash_functions and .bash_aliases files to find what I need.

Question(s):

How can I list functions/aliases available from the bash prompt?

Is it possible for me to document my bash functions/aliases using comments (kinda like PHPDoc)?

I'd just like a simple/nice way to output what's available without having to open the files. It would be cool to run a command and have it spit out a dynamic list of my functions/aliases (usage examples would be a plus). :)

3 Answers 3

18

To list active aliases, run:

alias

To see names of all active functions, run:

declare -F

To see the names and definitions of all active functions, run:

declare -f

More

The information on aliases is also available is a script-friendly format with:

declare -p BASH_ALIASES

man bash provides more info on the alias builtin:

   alias [-p] [name[=value] ...]
          Alias with  no  arguments  or  with  the  -p
          option  prints  the  list  of aliases in the
          form alias name=value  on  standard  output.
          When  arguments  are  supplied,  an alias is
          defined for each name whose value is  given.
          A  trailing  space in  value causes the next
          word to be checked  for  alias  substitution
          when  the  alias is expanded.  For each name
          in the argument list for which no  value  is
          supplied, the name and value of the alias is
          printed.  Alias returns true unless  a  name
          is   given  for  which  no  alias  has  been
          defined.

Regarding functions, man bash explains that declare can provide still more information is available if the extdebug option is set:

   Function  names  and definitions may be listed with
   the -f option to the  declare  or  typeset  builtin
   commands.  The -F option to declare or typeset will
   list the function names only  (and  optionally  the
   source  file and line number, if the extdebug shell
   option is enabled).

Links

  1. http://ss64.com/bash/alias.html
  2. http://linfo.org/alias.html
5
  • Ha! Too easy. Thanks! That's pretty easy. Any tips on listing custom functions?
    – mhulse
    Commented May 9, 2014 at 23:08
  • 1
    @mhulse Your welcome. See update for functions.
    – John1024
    Commented May 9, 2014 at 23:11
  • Thanks for the update! I see you added the declare info. Thank you! I could live with declare and alias for quick and easy viewing. I just noticed that I can do declare -f tree which spits out only the tree function. Cool! I'm sold. Thanks again! (I can accept this as answer in 4 minutes.)
    – mhulse
    Commented May 9, 2014 at 23:13
  • 2
    @ashumeow The ss64.com's text at ss64.com/bash/alias.html with their copyright and distribution terms ss64.com/docs/copyright.html (non-commercial!) BREAK the terms of GFDL--the license of the bash manual, because they include text from gnu.org/software/bash/manual/html_node/Aliases.html: commercial use of the derivative works of the Bash manual should be allowed. Their compilation without references doesn't seem nice for this and similar attribution reasons. Commented May 10, 2014 at 13:14
  • You may find the technique illustrated in Simpler processing of shell script options to be of use. Commented May 15, 2014 at 21:43
8

I use the following function and javadoc like comments to create a --help option for my scripts:

PROG=$0 #The program name, used within doHelp

# Print a help message
# doHelp uses lines starting with ## to create the output
# the tags {@param ...} and {@code ...} colorize words
doHelp() {
grep '^##' "${PROG}" |
sed -e 's/^##[[:space:]]*//' |
while read line; do
    if ( echo "${line}" | grep -q '{@param [^}]*}' ); then
        # color parameter and echo evaulated value
        eval echo -e $(echo ${line} | sed \
            -e 's/^\(.*\){@param \([^}]*\)}\(.*\)$/\
            \"\1\\\\E[32;40m\2\\\\E[37;40m\\t(value: \"$\2\")\3\"/');
    else
        # other color commands
        echo -e $(echo ${line} | sed \
            -e 's/{@code \([^}]*\)}/\\E[36;40m\1\\E[37;40m/g');
    fi
done;
}

At https://github.com/kaspervandenberg/aida/blob/master/Search/zylabPatisClient/src/main/scripts/generateReport.sh you can see how it's used within an actual script.

6
  • This is really cool! I wish I could give out green check marks for multiple answers. Thanks Kasper! I can't wait to try this out. :)
    – mhulse
    Commented May 12, 2014 at 20:21
  • I keep getting grep: : No such file or directory when trying to run it via unix/bash as a function. … I know this question is old, but could you give an example as to how one would run this just as only a bash function via command line? Thanks!!! :)
    – mhulse
    Commented Aug 4, 2014 at 2:55
  • 1
    @mhulse, I forgot to mention that you need to define PROG=$0; answer updated. Commented Aug 11, 2014 at 15:25
  • Thanks Kasper! I really want to get this to work, but I'm not having any luck so far. I'd hate to keep bugging you, but could you provide an example call from bash command line? Also, how could I set up --help or -help (i.e., if echo "$@" | egrep -q -e '(-h)|(--help)'; then .... I'd love to setup my .bash_functions/aliases to allow for aliasname -h or function arg --help. Thanks again!
    – mhulse
    Commented Aug 28, 2014 at 19:56
  • 1
    @mhulse The example call from the commandline generateReport.sh itself is only useful if you like to index medical documents via Zylab and query them via Aida. However, to try the help function use the following: wget https://raw.githubusercontent.com/kaspervandenberg/aida/master/Search/zylabPatisClient/src/main/scripts/generateReport.sh && chmod a+x generateReport.sh && ./generateReport.sh --help. How to achieve the second part with using aliasname in .bash_functions I do not (yet) know. Commented Aug 30, 2014 at 18:06
0

I have been self-documenting bash functions in my ~./bashrc with the : (null) built-in. The benefit is that it won't execute anything when run, but will show when printed.

$ declare -f my_function
my_function() {
    : My function does this and that
    echo "foo"
}
$ my_function
foo

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.