Friday, April 20, 2012

Shell scripting: export test1="a b c" works, but $(echo 'export test2="a b c"') doesn't

I seem to be having issues with some shell scripting (ZSH in particular), where the shell fails to understand exports with spaces in them. The problem only happens when `runcommand` or $(runcommand) is used.



Test case:



# Works fine:
export test1="a b c"

# Does not work:
$(echo 'export test2="a b c"')


The error is something along the lines of export:4: not valid in this context: c". Adding 1-2 \-escapes before the spaces and/or the quotes might change the error message to export:4: not valid in this context: b\.



Might anyone have insight into what the problem is? Thank you.



(The reason I am doing this is a hack to allow python to set shell variables by dynamically generating code which gets run in .myshellrc; if anyone knows of a more elegant way to do this, that would be quite welcome as a comment. Thank you.)



(*sigh*, I hope this isn't a version-specific issue in 4.3.12... I think this may have worked in the past.)





No comments:

Post a Comment