简体   繁体   中英

How environment variables can be read from a file when going into a login shell with here-string and using the sudo command?

I am trying to write a small script that aims to login to a remote server, load environment variables and print one of them. (In the actual script, instead of an echo , the parameters that are read are to be used. For the sake of simplicity here I am using just echo .)

The structure of the script and the commands that I tried are as follows but unfortunately none succeeds:

ssh -i lightsail.pem ubuntu@production <<< '
  sudo echo $TEST_PARAMETER
  sudo sh -c "~/Environment/environment-variables.sh && echo $TEST_PARAMETER"
  sudo bash -c "~/Environment/environment-variables.sh && echo $TEST_PARAMETER"
  sudo bash -c "source ~/Environment/environment-variables.sh && echo $TEST_PARAMETER"
  sudo bash <<< "source ~/Environment/environment-variables.sh && echo $TEST_PARAMETER"
';

How environment variables can be read from a file when going into a login shell with here-string and using the sudo command?

If your environment variable is set for ubuntu and not root you will need to use sudo -E

-E Indicates to the security policy that the user wishes to preserve their existing environment variables

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM