I'm trying to set an environment variable for a user session before running a script:
su - myUser -c "export myVar=$toto ; sh scriptThatNeedsMyVar.sh"
The scripts fails, I tried to debug and print myVar value:
su - myUser -c "export myVar=$toto ; echo $myVar"
returns nothing, that means that my env variable wasn't created although I'm creating and printing it in the same bash !
Then when I login as myUser
and type each command in bash console it works well
$su - myUser
$export myVar=toto
$echo $myVar
toto
I'm I missing something ?
Why is this happening and how can I export a temporary variable as another user ( I'm not allowed to edit bashrc or any other system file)
su - myUser -c 'export myVar=toto ; echo $myVar'
. No idea about the script file, that is probably a different issue. – Byte Commander♦ May 7 '18 at 16:15echo $myvar
within the double quotes. – Byte Commander♦ May 7 '18 at 16:27