daiktas
daiktas

Reputation: 23

Python argument parsing from bash variable

I'm trying to pass a bash variable to be parsed by a python script, as follows:

#!/bin/bash

command="--var1 var1 --var2 'var 2'"
echo $command
python test.py $command

python script (test.py)

import argparse

parser = argparse.ArgumentParser()

parser.add_argument("--var1", action="store", dest="var1")
parser.add_argument("--var2", action="store", dest="var2")

args = parser.parse_args()
print args.var1, args.var2

However, it fails due to there being whitespace, which I'm not sure how to escape error: unrecognized arguments: 2'. Running python test.py --var1 var1 --var2 'var 2' from the command line works fine though. Is there any way around this?

Upvotes: 1

Views: 1109

Answers (2)

kojiro
kojiro

Reputation: 77107

When you use an expansion without quotes, bash expands it, including any glob expansions or other things found within. It also wrecks any whitespace. This is why using quotes is so very recommended.

In other words, by the time the arguments get to python, they're already wordsplit, and can't be put back together.

If you need to put a set of arguments in a name, consider using an array, like

command=('--var1' 'var1' '--var2' 'var 2')

and the expansion like

python test.py "${command[@]}"

Using an array, bash will respect the argument setup just like it was in the array.

Upvotes: 1

John Kugelman
John Kugelman

Reputation: 361605

Use arrays to store multi-word strings. Regular string variables suffer from unavoidable whitespace and quoting problems whereas arrays handle them without issue.

command=(--var1 var1 --var2 'var 2')
echo "${command[@]}"
python test.py "${command[@]}"

"${command[@]}" is the syntax for expanding an array while preserving each element as a separate word. Make sure to use the quotes; don't leave them out.

Upvotes: 6

Related Questions