I have a system that places a quoted string in an environment variable. This environment variable is passed to a python program as a command-line argument, but the quoted string gets split into multiple words instead of staying together.
For example:
In the shell:
$ export ARG_OPTIONS='--flag -a 31 --big_complicated_flag "How are you?"'
In the bash script:
python args.py $ARG_OPTIONS
In the python script:
import sys
print sys.argv
And the resulting output is:
['args.py', '--flag', '-a', '31', '--big_complicated_flag', '"How', 'are', 'you?"']
The important part here is that the quoted string has been split into multiple pieces, which is not how the arguments are expected by the program. I cannot change this setup too much, so any ideas would be appreciated.
Things I've Tried
Double Quotes in Shell
python args.py "$ARG_OPTIONS"
Result: ['args.py', '--flag -a 31 --big_complicated_flag "How are you?"']
This is incorrect, as the entire variable gets passed in as a single argument. However, I expected this behavior, but I thought I'd try.
Single Quotes in Shell
python args.py "$ARG_OPTIONS"
Result: ['args.py', '$ARG_OPTIONS']
Also incorrect, but expected from my understanding of single quotes.
Double Quotes around Environment Variable
export ARG_OPTIONS="--flag -a 31 --big_complicated_flag \"How are you?\""
Result: ['args.py', '--flag', '-a', '31', '--big_complicated_flag', '"How', 'are', 'you?"']
This result is the same as the original.
Single Quotes around Double Quotes around Environment Variable
export ARG_OPTIONS='"--flag -a 31 --big_complicated_flag \"How are you?\""'
Result: ['args.py', '"--flag', '-a', '31', '--big_complicated_flag', '\\"How', 'are', 'you?\\""']
Also incorrect.
Messing with IFS
Changing the IFS separator and manually separating the arguments in the environment variable:
export ARG_OPTIONS='--flag|-a|31|--big_complicated_flag|How are you?'
In the shell script:
IFS='|'
python args.py $ARG_OPTIONS
Result: ['args.py', '--flag', '-a', '31', '--big_complicated_flag', 'How are you?']
This produces the expected output, but it seems hackish and prone to confusion later on. Obviously, there is something I am not understanding about the interaction between the shell and the environment variable.
Why is it separating the words in the double quotes? And is there a cleaner way to accomplish my goal?