# \`cmd\` and UTF-8

**URL:** <https://www.ruby-forum.com/t/cmd-and-utf-8/65507>\
**Category:** Ruby\
**Created:** [August 18, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/cmd-and-utf-8/65507 "2006-08-18T20:35:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![atd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@atd](https://www.ruby-forum.com/u/atd)\
**Post date:** [August 18, 2006, 8:35pm UTC](https://www.ruby-forum.com/t/cmd-and-utf-8/65507/1 "2006-08-18T20:35:26Z")

</div>

I have spent several hours searching for this and I still don’t know how  
it works.

How is `cmd` string encoded? I want it in UTF-8, but I don’t know how to  
do it!  
I have tried with $KCODE = ‘u’ but it doesn’t works.  
My Ruby version is 1.8.2

---

<div class="post-metadata">

**Author:** ![atd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@atd](https://www.ruby-forum.com/u/atd)\
**Post date:** [August 18, 2006, 8:45pm UTC](https://www.ruby-forum.com/t/cmd-and-utf-8/65507/2 "2006-08-18T20:45:13Z")

</div>

On 18/08/06, Antonio M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> I have spent several hours searching for this and I still don’t know how  
> it works.
> 
> How is `cmd` string encoded? I want it in UTF-8, but I don’t know how to  
> do it!  
> I have tried with $KCODE = ‘u’ but it doesn’t works.  
> My Ruby version is 1.8.2

Do you mean the return value? That depends on your platform, locale,  
and, possibly, the program itself. If you know the encoding, you can  
use Iconv to convert it to UTF-8:

require ‘iconv’  
s = `some cmd` # returns a string in iso-8859-1, for example  
iconv = Iconv.new(“utf-8”, “iso-8859-1”)  
iconv.iconv(s) # =\> string in UTF-8

Perhaps you could post a sample of code to show what you are trying to  
do. It’s easier to solve a concrete problem than to read minds.

Paul.

---

<div class="post-metadata">

**Author:** ![atd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@atd](https://www.ruby-forum.com/u/atd)\
**Post date:** [August 18, 2006, 11:48pm UTC](https://www.ruby-forum.com/t/cmd-and-utf-8/65507/3 "2006-08-18T23:48:31Z")

</div>

> Yes, I mean the return value.
> 
> I can’t use Iconv because the script should work with any locale.

Until Ruby gets some non-basic Unicode support, I -think- you’re stuck  
to iconv-ing. I don’t think that even $KCODE=‘u’ will detect user input  
locale and convert automatically, but only changes the behaviour of  
string manipulation code. I could be completely wrong though.

You should be able to detect the current locale using the standard  
library, I haven’t really done this though. Welcome to the world of text  
encoding, may whoever invented codepages burn in hell forever for that.

David V.

---

<div class="post-metadata">

**Author:** ![atd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@atd](https://www.ruby-forum.com/u/atd)\
**Post date:** [August 18, 2006, 9:04pm UTC](https://www.ruby-forum.com/t/cmd-and-utf-8/65507/4 "2006-08-18T21:04:02Z")

</div>

Paul B. wrote:

> On 18/08/06, Antonio M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > I have spent several hours searching for this and I still don’t know how  
> > it works.
> > 
> > How is `cmd` string encoded? I want it in UTF-8, but I don’t know how to  
> > do it!  
> > I have tried with $KCODE = ‘u’ but it doesn’t works.  
> > My Ruby version is 1.8.2
> 
> Do you mean the return value? That depends on your platform, locale,  
> and, possibly, the program itself. If you know the encoding, you can  
> use Iconv to convert it to UTF-8:
> 
> require ‘iconv’  
> s = `some cmd` # returns a string in iso-8859-1, for example  
> iconv = Iconv.new(“utf-8”, “iso-8859-1”)  
> iconv.iconv(s) # =\> string in UTF-8
> 
> Perhaps you could post a sample of code to show what you are trying to  
> do. It’s easier to solve a concrete problem than to read minds.

Yes, I mean the return value.

I can’t use Iconv because the script should work with any locale.  
This is the code:

require ‘rexml/document’

[…]

include REXML

@atom = Document.new  
entry = Element.new ‘entry’  
entry.attributes[‘xmlns’] = ‘[Atom Syndication Format namespace](http://www.w3.org/2005/Atom)’  
@atom.elements \<\< entry  
@atom \<\< XMLDecl.new

e = Element.new ‘title’  
e \<\< Text.new(`dcop amarok player title`)  
entry \<\< e

XML tag text should be in UTF-8.

Thank you.

---

<div class="post-metadata">

**Author:** ![atd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@atd](https://www.ruby-forum.com/u/atd)\
**Post date:** [August 21, 2006, 1:16am UTC](https://www.ruby-forum.com/t/cmd-and-utf-8/65507/5 "2006-08-21T01:16:27Z")

</div>

Antonio M. wrote:

> I have spent several hours searching for this and I still don’t know how  
> it works.
> 
> How is `cmd` string encoded? I want it in UTF-8, but I don’t know how to  
> do it!  
> I have tried with $KCODE = ‘u’ but it doesn’t works.  
> My Ruby version is 1.8.2

Here is a solution:

ENV[“LC\_ALL”] = “C.UTF-8”  
utf8\_string = `cmd`

Thank you all!

---

<div class="post-metadata">

**Author:** ![atd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/dc4da7/32.png) [@atd](https://www.ruby-forum.com/u/atd)\
**Post date:** [August 19, 2006, 4:16am UTC](https://www.ruby-forum.com/t/cmd-and-utf-8/65507/6 "2006-08-19T04:16:02Z")

</div>

Hi,

At Sat, 19 Aug 2006 04:04:03 +0900,  
Antonio M. wrote in [ruby-talk:209287]:

> I can’t use Iconv because the script should work with any locale.

Then what encoding does your target program use?

If it is affected by environment locale, it may use utf-8 by  
setting ENV[“LC\_ALL”] etc.
