I want to transfer all environment variables of the one shell (in my case: kornshell) to another shell (in my case: the z-shell) automatically.
If possible, the transfer should be in the startup file of the zshell to aviod the use of additional scripts as I want to transfer it to other servers for the same purpose.
What I tried so far:
put $ export $(ksh -c env | tr '\n' ' ') in the .zshrc (Startupfile of the Zshell).
This was not working because the command is executed as a child of the current shell (zsh) which consequently has the same environment variables as the zsh and NOT the environment of the kornshell.
in an extra script
#!/usr/bin/ksh
echo $(ksh -c env | tr '\n' ' ') # for testing if it works
export $(ksh -c env | tr '\n' ' ')
This doent's work either.
Any comments are highly appreciated.
export $(ksh -c env | tr '\n' ' ')
does not work for me because "env" returns text like:
key1=value1 value2
...
So the above command will expand $(ksh -c env | tr '\n' ' ') and run:
export key1=value1 value2
But this generates error:
export: value2: is not an identifier
Note that there may be other bad characters on the right side of "=" sign in the output of "env", which will produce errors.
But there is a better way to export environment:
export -p > myenv.sh
this outputs in Bash
declare -x key1="value1 value2"
....
It appears that Bash "declare -x" is the same as "export". But in ksh "export -p" prints
export key1='value1 value2'
...
Now I just need to run
source myenv.sh
to export all variables
But I also need to delete all existing variables before exporting new. I found solution at: http://www.linuxquestions.org/questions/linux-newbie-8/how-to-unset-environment-variable-in-bash-383066/
So, my final code is:
unset `env | awk -F= '/^\w/ {print $1}' | xargs`
source myenv.sh
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With