Get users home directory when they run a script as root

前端 未结 5 969
离开以前
离开以前 2021-01-31 03:26

I have a sh script that needs to be run as root, however it is run by the end user using sudo. How can I get the users home directory when ~/ points to /root when running with s

5条回答
  •  天命终不由人
    2021-01-31 04:09

    The user's home directory would be ~$SUDO_USER. You can use eval as follows:

    USER_HOME=$(eval echo ~${SUDO_USER})
    echo ${USER_HOME}
    

提交回复
热议问题