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)
Best Answer
use
env
:Assuming, of course, that
scriptThatNeedsMyVar.sh
is executable, and findable inmyUser
'sPATH
, and begins with#!/bin/sh
.