magulla magulla - 1 month ago 5x
Linux Question

korn shell and environment variables

env and start.env that should run in any shell.
It actually does except for KORN where env variable setting is not behaves the way I would expect. So look at example.

file set.env :

export MY_VAR="home" || setenv MY_VAR "home"

file :

command . ./set.env || source set.env
echo "$MY_VAR"

i can see the print of variable.
but if try to echo it in terminal under ksh, it turned to be not defined.

ksh$ echo $MY_VAR


I would expect to see $MY_VAR in my session... any ideas ?
//run under red hat


When you run, you're executing it as a subcommand, not sourcing it. Consequently, changes it makes to environment variables are scoped to that process and its children; once the process exits, the environment variables it sets die with it.

To portably source the script, executing it in your current shell and thus setting environment variables within that shell, run:

# this works on any POSIX shell, including ksh (and bastardizations such as mksh)

...or, less portably:

# this is a bashism

BTW, as a practice, command . ./set.env is... odd. command prevents execution of shell functions, but any environment where a function named . is defined is arguably a buggy environment. Consider . alone.