I have a Python script script.py which I am using to generate the command line argument to another script exactly like so:
./main $(./script.py)
The output of script.py may contain spaces (e.g. foo bar) which are being unintentionally interpreted by the shell. I want the argument to ./main to be the single string "foo bar". Of course I can solve this problem if I quote the argument to ./main, like this:
./main "$(./script.py)"
But I can't and don't want to do that. (The reason is because ./main is being called without quotes from another script which I don't have control to edit.)
Is there an alternative representation of the space character that my Python script can use, and that bash won't interpret?
You can try to have ./script.py output a non-breaking space (U+00a0) instead of a regular space, which bash will not use for word-splitting. However, I would file a bug report to have the script that calls main add quotes to its argument. Whether this works depends on how main reacts to getting a string that consists of a two-byte UTF-8 sequence representing U+00a0 rather than a single space character.
A sample script.py:
#!/usr/bin/python
print u'foo\xa0bar'.encode('utf8')
A sample script a.bash:
#!/bin/bash
main () {
echo $#
}
main $(script.py)
And finally, a demonstration that main gets 1 argument from the output of script.py:
$ bash a.bash
1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With