MacOS显示Bash定义函数,并查看函数定义

在Mac OS中,用declare -F可以显示所有Bash的定义过的函数。
例如:

$ declare -F
declare -f shell_session_delete_expired
declare -f shell_session_history_allowed
declare -f shell_session_history_check
declare -f shell_session_history_enable
declare -f shell_session_save
declare -f shell_session_save_history
declare -f shell_session_update
declare -f update_terminal_cwd

如果想要查看某个函数的完整定义,可以用命令type function_name。例如:

$ type update_terminal_cwd
update_terminal_cwd is a function
update_terminal_cwd () 
{ 
    local url_path='';
    { 
        local i ch hexch LC_CTYPE=C LC_ALL=;
        for ((i = 0; i < ${#PWD}; ++i))
        do
            ch="${PWD:i:1}";
            if [[ "$ch" =~ [/._~A-Za-z0-9-] ]]; then
                url_path+="$ch";
            else
                printf -v hexch "%02X" "'$ch";
                url_path+="%${hexch: -2:2}";
            fi;
        done
    };
    printf '\e]7;%s\a' "file://$HOSTNAME$url_path"
}

你可能感兴趣的:(MacOS显示Bash定义函数,并查看函数定义)