0

I'm trying to set the environment variable within my mingw gitbash (windows7-x64) via a small bash script. But it doesn't get set, only if I execute it manually.

contents of dev.bash

schwat@AACarrier MINGW64 ~/Documents/test
$ cat dev.bsh

#!/usr/bin/env sh
export KUBECONFIG="/c/Users/schwat/Documents/test/.kube/dev.kubecfg"
kubectl config set-context dev --cluster=kubernetes --namespace=dev --user=admin
kubectl config use-context dev
echo "Connected to ENV:DEV"

executed dev.bsh and echo of $KUBECONFIG

schwat@AACarrier MINGW64 ~/Documents/test
$ ./dev.bsh
Context "dev" modified.
Switched to context "dev".
Connected to ENV:DEV


schwat@AACarrier MINGW64 ~/Documents/test
$ echo $KUBECONFIG

exporting KUBECONFIG manually and echo of $KUBECONFIG

schwat@AACarrier MINGW64 ~/Documents/test
$ export KUBECONFIG="/c/Users/schwat/Documents/test/.kube/dev.kubecfg"


schwat@AACarrier MINGW64 ~/Documents/test
$ echo $KUBECONFIG
/c/Users/schwat/Documents/test/.kube/dev.kubecfg

Any idea what's wrong here? (not a duplicate of: Set an environment variable in git bash)

Thomas Schwärzl
  • 9,518
  • 6
  • 43
  • 69

1 Answers1

1

I see two main points in your script:

First you are using sh instead of bash in your script

#!/usr/bin/env sh

#!/usr/bin/env bash

And the second point that I see is related to the understanding of the export in a script. When you execute a script a new process is created so the variables you create and export is available to this new process and to all possibles sub-process, and not to the parent process, in this case the shell which you call your script.

So, you variable is probably being create but when the script finish it is destroyed and you cannot see it anymore.

Hope it helps!