How do I list the functions defined in my shell?

后端 未结 8 2034
爱一瞬间的悲伤
爱一瞬间的悲伤 2020-12-12 09:14

I can type alias to show a list of all the aliases.

But for functions, all I can do is grep my .bash_profile.

That only gets the ones in that fi

8条回答
  •  孤街浪徒
    2020-12-12 09:43

    Bash shell script specific answer

    (because this question was marked as a duplicate of this one)

    If you have functions exported in your shell a called script will see them. But luckily, they get marked as such.

    Copy-Pasta Demo script

    #!/bin/bash
    foo(){ :;}; bar(){ :;}; baz(){ :;}; export -f foo bar
    
    export -pf
    
    bash -c $'spam(){ :;}; \
    echo "RAW:"; \
    declare -pF; \
    echo "FILTERED:"; \
    declare -pF | awk \'$2 !~ /x/{print$3}\''
    

    Transcript in Bash

    /tmp $ foo(){ :;}; bar(){ :;}; baz(){ :;}; export -f foo bar
    
    /tmp $ export -pf
    bar ()
    {
        :
    }
    declare -fx bar
    foo ()
    {
        :
    }
    declare -fx foo
    
    /tmp $ bash -c $'spam(){ :;}; echo "RAW:"; declare -pF; echo "FILTERED:"; declare -pF | awk \'$2 !~ /x/{print$3}\''
    RAW:
    declare -fx bar
    declare -fx foo
    declare -f spam
    FILTERED:
    spam
    

提交回复
热议问题