0

this is my first post,

I've some questions about su - [user] -c [command]

I've made some searches, I found this thread and this other one who are related to my question.

I'm trying to set an environment variable of a user if it doesn't exists.

# This function checks for a user $1, a var nammed $2 to $3 and set it to $3 if $2 not set.
# $1: User to set env
# $2: Var to set
# $3: Value to add in $2 if not exist in $2
function setEnvIfNotDef () {
  if [ ! su ${1} -c "echo "${!2}" | grep "${3}"" ]
  then
    if [ ${2} = "PATH" ]
    then
        su ${1} -c "export ${2}=${!2}:${3}"
    else
        su ${1} -c "export ${2}=${3}"
    fi
  fi
}
setEnvIfNotDef ${USER_NAME} "TEST_HOME" ${INSTALL_PATH}

(USER_NAME and INSTALL_PATH are defined in my code ^^)

The problem is, when I try su - root -c command in my terminal on Yosemite/CentOS7.

Elio@Hackintosh-Elio [16:43:41] [Elio] #7975 $ su - root -c "whoami"
Password:
root
Elio@Hackintosh-Elio [16:43:59] [Elio] #7976 $ su - root -c "echo $HOME"
Password:
/Users/Elio

The - after su equals -l right ? I'm not supposed to be in root's environment ?

Little help please ? :)

Thank's

1 Answers1

3

Use single apostrophe instead of the double one:

$ su - -c 'echo $HOME'                            
Password: 
/root
Ipor Sircer
  • 14,546
  • 1
  • 27
  • 39