I am trying to call exec with an argument that contains multibyte characters that come from an environment variable in Windows but have not yet found a solution that works. Here is what I have been able to debug so far.
For simplicity, suppose I have a directory called "Seán" that I am trying to use as an argument to exec. If I just call
exec 'script', "Se\u00E1n".encode("IBM437")
The script, which is exec'ed, cannot find the file because the argument is configured so that the shock character is lost. If I do the following, this works, but this is bad practice, since the argument must be escaped before it goes into the shell.
exec "script #{"Se\u00E1n".encode("IBM437")}"
So, I thought I was just using shellescape to protect the use of exec.
require 'shellwords' exec "script #{"Se\u00E1n".encode("IBM437").shellescape}"
But the problem is that he eludes a special character so that it looks like this: "Se \ án". I figured out where this happens, and this comes from this regex .
str.gsub!(/([^A-Za-z0-9_\-.,:\/@\n])/, "\\\\\\1")
At first glance, it seems that the characters are not in the well-known good character set of the shell. Unfortunately, this set does not contain special characters, so I run into problems.
I am looking for a regular expression that will perform shell escaping that will not spoil special characters so that I can avoid these arguments before passing them to exec.