user14389292
user14389292

Reputation:

Bash - how to process additional parameter in function?

I have the following bash script but im unable to use my defined command "kctl" if i call it with "get pods". Do I maybe have to add a placeholder behind the command at the kctl() funtction or so?

    #!/bin/bash
    KCTL=$(which kubectl)
    KUBECONFIG=$1

    kctl() {
      $KCTL --kubeconfig=$KUBECONFIG
    }

    kctl get pods

For some reason I always get back the kubectl help and not a list of pods at the default namespace.

Thanks in advance

Upvotes: 0

Views: 63

Answers (1)

Miguel
Miguel

Reputation: 2219

Here ktcl is a function on its own, so it has its own parameters. When you use it in the end you pass additional parameters to it but then in the function you do nothing with them.

If your intention is to append the arguments to the command inside the function, you have to write it like this:

kctl() {
      $KCTL --kubeconfig=$KUBECONFIG "$@" 
}

Notice how the function doesn't see the arguments of the script, as they are shadowed by its own arguments.

Upvotes: 1

Related Questions