0

I defined a nested function in my .zshrc hoping to define multiple similar zsh functions :

makefunc_gman () {
    local MAN_SEC_LIST=(1 2 3 4 5 6 7 8) 
    for GMAN_SEC in ${MAN_SEC_LIST}
    do
        "gman${GMAN_SEC}" () {
            gdir_C "$1" "man${GMAN_SEC}" 
        }
    done
}

makefunc_gman # calls the function
  • The functions I wanted to define the function above are gman1, gman2 to gman8.
  • I want to get a function named gman1 which calls gdir_C "$1" man1 after sourcing this function above.
  • gdir_C in the code is another function defined in .zshrc.

But when I sourced .zshrc, where gman1 gave me this:

$ where gman1        
gman1 () {
    local MAN_GMAN_SEC="man${GMAN_SEC}" 
    gdir_C "$1" "man${GMAN_SEC}"
}

It seemed that the variable $GMAN_SEC in this nested function is not expanded. How do I get the variable to expand correctly? Thanks!

1 Answer 1

2

You would need to use eval, as zsh doesn't have nested scopes or closures.

makefunc_gman () {
    local MAN_SEC_LIST=(1 2 3 4 5 6 7 8) 
    for GMAN_SEC in ${MAN_SEC_LIST}
    do
        eval "gman${GMAN_SEC} () {
            gdir_C \"\$1\" man${GMAN_SEC}
        }"
    done
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.