Displaying functions

From Linux Shell Scripting Tutorial - A Beginner's handbook
Jump to: navigation, search
← Writing your first shell functionHomeRemoving functions →


To display defined function names use the declare command. Type the following command at a shell prompt:

declare -f

Sample outputs:

declare -f command_not_found_handle
declare -f genpasswd
declare -f grabmp3
declare -f hello
declare -f mp3
declare -f xrpm

Display Function Source Code

To view function names and source code, enter:

declare  -f

OR

declare -f | less

Sample outputs:

command_not_found_handle () 
{ 
    if [ -x /usr/lib/command-not-found ]; then
        /usr/bin/python /usr/lib/command-not-found -- $1;
        return $?;
    else
        return 127;
    fi
}
genpasswd () 
{ 
    local l=$1;
    [ "$l" == "" ] && l=16;
    tr -dc A-Za-z0-9_ < /dev/urandom | head -c ${l} | xargs
}
grabmp3 () 
{ 
    local t=$($HOME/bin/mp3 | sed  's/^n//');
    grep -q "$t" $HOME/out/best.eng.mp3
    if [ $? -ne 0 ]; then
        echo "$t" >> $HOME/out/best.eng.mp3;
        echo "'$t' - added!";
    else
        echo "Duplicate entry found!";
    fi
}
hello () 
{ 
    echo "Hello $1"
}
mp3 () 
{ 
    local o=$IFS;
    IFS=$(echo -en "\n\b");
    /usr/bin/beep-media-player "$(cat  $@)" & IFS=o
}
xrpm () 
{ 
    [ "$1" != "" ] && ( rpm2cpio "$1" | cpio -idmv )
}

To view a specific function source code, enter:

declare  -f functioName
declare  -f xrpm

Notice if you just type the declare command with no arguments, then it will list all declared variables and functions.

See also

← Writing your first shell functionHomeRemoving functions →