Bash 如何在使用匹配模式时将函数用作字符串

Bash 如何在使用匹配模式时将函数用作字符串,bash,function,shell,Bash,Function,Shell,我想利用函数获取脚本的完整路径和目录名。 为此,我创建了两个函数: function _jb-get-script-path () { #returns full path to current working directory # + path to the script + name of the script file return $PWD/${0#./*} } function _jb-get-script-dirname () { return

我想利用函数获取脚本的完整路径和目录名。 为此,我创建了两个函数:

function _jb-get-script-path ()
{
    #returns full path to current working directory 
    # + path to the script + name of the script file
    return $PWD/${0#./*}
}

function _jb-get-script-dirname ()
{
    return ${(_jb-get-script-path)##*/}
}
as$(_jb-get-script-path)应替换为调用的函数的结果。 但是,我得到一个错误:
${(_jb-get-script-path)##*/}:错误的替换

因此,我尝试了另一种方法:

function _jb-get-script-path ()
{
    return $PWD/${0#./*}
}

function _jb-get-script-dirname ()
{
    local temp=$(_jb-get-script-path);
    return ${temp##*/}
}
但在本例中,第一个函数会导致错误:
需要数值参数
。我试图运行
localtemp=$(\u jb-get-script-path$0)
,以防$0不是通过函数调用提供的(或者我不知道为什么),但它没有改变任何东西

我不想复制第二个功能的内容,因为我不想无缘无故地复制代码。
如果你知道这些错误发生的原因,我真的很想知道原因,当然,如果你有更好的解决方案,我很乐意听到。但是我对这个问题的解决非常感兴趣。

您需要使用
echo
而不是用于返回数字状态的return:

_jb-get-script-path() {
    #returns full path to current working directory
    # + path to the script + name of the script file
    echo "$PWD/${0#./*}"
}

_jb-get-script-dirname() {
    local p="$(_jb-get-script-path)"
    echo "${p##*/}"
}

_jb-get-script-dirname

有没有不使用
basename
dirname
的原因?没有,但现在我想了解这里到底发生了什么。另外,在bash函数中,函数不返回值,它们返回退出状态,应该在0到255之间。确实如此。我结束这个问题