erlang os:cmd() command with UTF8 binary

404 Views Asked by At

I'm trying to get an Erlang function to execute a bash command containing unicode characters. For example, I want to execute the equivalent of:

touch /home/jani/ჟანიweł

I put that command in variable D, for example:

io:fwrite("~ts", [list_to_binary(D)]).                                                                       
touch /home/jani/ჟანიwełok

but after I execute:

os:cmd(D)

I get file called á??á??á??á??weÅ?. How can I fix it?

os:cmd(binary_to_list(unicode:characters_to_binary("touch /home/jani/编程"))).

Executing this command creates a file named ��, while executing the equivalent touch command directly in a terminal creates the file with the correct name.

1

There are 1 best solutions below

0
On BEST ANSWER

Its because Erlang reads your source files like latin1 by default, but on newer versions of erlang you can set your files to use unicode.

%% coding: utf-8

-module(test).
-compile(export_all).

test() ->
    COMMAND = "touch ჟანიweł",
    os:cmd(COMMAND).

and then compiling and executing the module works fine

rorra-air:~ > erl
Erlang/OTP 17 [erts-6.4] [source] [64-bit] [smp:4:4] [async-threads:10] [hipe] [kernel-poll:false] [dtrace]

Eshell V6.4  (abort with ^G)
1> c(test).
{ok,test}
2> test:test().
[]

and it created the file on my filesystem

rorra-air:~ > ls -lta
total 144
-rw-r--r--   1 rorra  staff      0 Jun  9 15:18 ჟანიweł