I have a bash script which reads variable from environment and then passes it to the python script like this
#!/usr/bin/env bash
if [ -n "${my_param}" ]
then
my_param_str="--my_param ${my_param}"
fi
python -u my_script.py ${my_param_str}
Corresponding python script look like this
parser = argparse.ArgumentParser(description='My script')
parser.add_argument('--my_param',
type=str,
default='')
parsed_args = parser.parse_args()
print(parsed_args.description)
I want to provide string with dash characters "Some -- string" as an argument, but it doesn't work via bash script, but calling directly through command line works ok.
export my_param="Some -- string"
./launch_my_script.sh
gives an error unrecognized arguments: -- string and
python my_script.py --my_param "Some -- string" works well.
I've tried to play with nargs and escape my_param_str="--my_param '${my_param}'" this way, but both solutions didn't work.
Any workaround for this case? Or different approach how to handle this?
--marks the end of arguments for a command in the normal case.sys.argvto see whatbashis giving your script.argparsecan only parse the strings in that list.