Can you pass an environment variables into kubectl exec without bash -c?

Just use sh:

kubectl -n nmspc exec "$POD" -- /bin/sh -c 'curIP=123 script01'

/usr/bin/env exports values passed in key=value pairs into the environment of any program it's used to invoke.

kubectl -n nmspc exec "$POD" -- env curIP=123 script01

Note that you should never use $runScript or any other unquoted expansion to invoke a shell command. See BashFAQ #50 -- I'm trying to put a command in a variable, but the complex cases always fail!


As an example of how you could keep bash -c in place but have your command work, consider:

runScript() {
  kubectl -n nmspc exec "$POD" -- bash -c 'export curIP=123 && script01 "$@"' _ "$@"
}

runScript --command "do stuff"

Here, runScript is a function, not a string variable, and it explicitly passes its entire argument list through to kubectl. Similarly, the copy of bash started by kubectl explicitly passes its argument list (after the $0 placeholder _) through to script01, so the end result is your arguments making it through to your final program.