0

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

1 Answers1

1

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.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
Miguel
  • 2,130
  • 1
  • 11
  • 26
  • 1
    You're correct, but you probably want to use `"%@"` to ensure parameters are correctly preserved (with spaces and so on). For example, `kctl get pods --namespace "my space"` won't work well since `my space` will be word-split within the function. – paxdiablo May 12 '21 at 09:06
  • 1
    Sorry, Miguel, that `%` was a typo in my comment, it should have been `$` - I've fixed your answer to use the latter. – paxdiablo May 12 '21 at 09:11