Search code examples
kuberneteskubectlkubernetes-pod

K8S How to add quotes when on parameters provided for `kubectl exec -c`


I am using K8S

I want to calculate a string that that is a result of kubectl exec -it ... -c

after the -c option there is a string.

How can I pass a string with double quotes inside.

The following example doesn't work properly.

x="$(kubectl exec -it mysql-pod -- /bin/sh -c \"mysql -uroot -p12345
   -e 'show databases'\" 2>/dev/null)"
echo $x

Thanks.


Solution

    • when only a command needs to be executed on a pod , -it option is not required as it stands for attaching an interactive teminal
    • when mysql is itself an executable command , no need to use /bin/sh -c
    • no need to encapsulate whole command in " "

    So try following

    x=$(kubectl exec mysql-pod -- mysql -uroot -p12345 -e 'show databases ;' 2>/dev/null)
    echo $x