title: How do I run a non-ASCII/Unicode shell command from Ruby on Windows?
body: <p>I cannot figure out the proper way to encode a shell command to run from Ruby on Windows. The following script reproduces the problem:</p>
  
  <pre><code># encoding: utf-8
  
  def test(word)
    returned = `echo #{word}`.chomp
    puts "#{word} == #{returned}"
    raise "Cannot roundtrip #{word}" unless word == returned
  end
  
  test "good"
  
  test "bÃd"
  
  puts "Success"
  
  # win7, cmd.exe font set to Lucinda Console, chcp 65001
  # good == good
  # bÃd == bÃƒd
  </code></pre>
  
  <p>Is this a bug in Ruby, or do I need to encode the command string manually to a specific encoding, before it gets passed to the cmd.exe process?</p>
  
  <p><strong>Update</strong>: I want to make it clear that the problem is not with reading the output back into Ruby, its purely with sending the command to the shell. To demonstrate:</p>
  
  <pre><code># encoding: utf-8
  
  File.open("bbbÃd.txt", "w") do |f|
    f.puts "nothing to see here"
  end
  
  filename = Dir.glob("bbb*.txt").first
  command = "attrib #{filename}"
  
  puts command.encoding
  
  puts "#{filename} exists?: #{ File.exists?(filename) }"
  system command
  File.delete(filename)
  
  #=&gt;
  # UTF-8
  # bbbÃd.txt exists?: true
  # File not found - bbbÃƒd.txt
  </code></pre>
  
  <p>You can see that the file gets created correctly, the <code>File.exists?</code> method confirms that Ruby can see it, but when I try to run the <code>attrib</code> command on it, its trying to use a different filename.</p>