简体   繁体   中英

Exporting environment variables with spaces

I would like to have a consistent way of dealing with environment variables. For example I have a file .env.development containing:

# Some comment to be ignored
VAR0='some""" value0'
VAR1="some value1"
VAR2=ignored_value
VAR2=some_value2

From a bash shell I can set the shell variables (not the environment variables):

$ source .env.development
$ echo $VAR0
some""" value0
$ echo $VAR1
some value1

I would also like to make these variables available to subprocesses launched from the shell:

script.py

#!/usr/bin/python
import os
print("VAR0 is:", os.environ.get("VAR0")

As such I need to export these variables. Using the following is close but retains the quotes needed to allow sourcing the file:

$ IFS=$'\n' && export $(grep -v ^# .env.development | xargs -0) && unset IFS && ./script.py
('VAR0 is:', '\'some""" value0\'')

I'd like to get the output:

('VAR0 is:', 'some""" value0')

You can ignore grep, xargs and IFS and use set -a with source :

$ set -a && source .env.development && set +a && ./script.py 
('VAR0 is:', 'some""" value0')

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