Search code examples
linuxshellparameter-passingzshalias

ZSH Shell: Executable alias from function parameter


i tried to execute an alias which is given via parameters on a function. But i can't execute the alias. I also can't find a solution for it. Anyone have an idea for this case?

This works:

alias rabbitmq='cd xy/_infra/v2/rabbitmq'

test () {
    echo "run alias"
    rabbitmq
}

This won't work:

alias rabbitmq='cd xy/_infra/v2/rabbitmq'

runAlias () {
    local aliasCmd=$1

    echo "run alias $aliasCmd"
    $aliasCmd
}

test () {
    runAlias rabbitmq
}

Output is this:

run alias rabbitmq
runAlias:4: command not found: rabbitmq

I tried to use this with quotes, brackets and with the function ${BASH_ALIASES[$alias_key]}, but nothing helped.

I would like to execute the alias which is given by parameter to a function.


Solution

  • Alias-expansion is done before parameter expansion. If you really want the alias to be understand, you would have to do

    eval "$aliasCmd"
    

    But I would rather recommend a redesign of your code so that you don't need an alias to be passed to a function.