简体   繁体   中英

Using environmental variables in bash while executing python script?

I exported variable in local environment, and need to pass that variable into bash script, which in turn will need to run python script using that env.

#!/usr/bin/env bash
export API_TOKEN=836176e9b6ce
sudo python script.py --pretty

Right now I am getting following:

Could not find values for Program api_token.
They must be specified via either ini file, command line argument (--api-token),
or environment variables (API_TOKEN)

Is it possible to achieve?

The environment you set in the script is not passed through to the command executed by sudo . To preserve environment variables, use the -E flag:

#!/usr/bin/env bash
export API_TOKEN=836176e9b6ce
sudo -E python script.py --pretty

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