修改shell函数的参数?

时间:2015-10-29 17:50:47

标签: bash shell

我对如何修改功能争论感到困惑:

我想:

bower ln
bower ln thing

执行:

bower link
bower link thing

这就像我得到的那样接近:

# ~/.bashrc
bower () {
    if [[ $1 == "ln" ]]; then
        ${@[1]}="link"
        echo "replacing" "$FUNCNAME" "$@"
    fi
    command "$FUNCNAME" "$@"   
}

1 个答案:

答案 0 :(得分:2)

使用set命令:

bower () {
    if [[ $1 == ln ]]; then
        set -- link "${@:2}"
        echo "replacing $FUNCNAME $@"
    fi
    command "$FUNCNAME" "$@"
}

但是,你真的不必替换第一个参数;你可以简单地在if语句的正文中硬编码正确的参数:

bower () {
    if [[ $1 == ln ]]; then
        echo "replacing $FUNCNAME $first $@"
        command "$FUNCNAME" link "${@:2}"
    else
        command "$FUNCNAME" "$@"
    fi
}