My system language is not Japanese, but my user language/encoding is. Using shell things in terminal emulators 'directly' works okay, but it seems that 'behind the scene' stuff does not.

保存 = Save for example.

This gets displayed correctly when I use X stuff 'directly' (as in, I open a terminal emulator or GUI program and mess with it directly) But running stuff in the background (using dmenu, in the xmonad WM ) it turns out that what gets fed into scripts is not something I can string compare to, but something like this:


gets turned into


How do I generate this myself? I only got the escaped value string from this by putting a copy paste thing into the script.

I'm fine with doing a pre-pass that translates such an escaped value string into a 'proper' one that then gets case matched later.

I would simply like to generate such an escaped value string myself without the roundabout hack.

  • Is it just the characters outside the ASCII range you want to convert or all of them. What should happen for control characters (like TAB, NL) or \ or ' themselves? – Stéphane Chazelas Nov 23 '20 at 14:52

With the zsh shell, with:

string="保存 = Save for example.
Also including newlines,
'quotes and \backslashes"

() {
  local LC_ALL=C
} "$string"

printf '%s\n' $quoted


'\344\277\235\345\255\230 = Save for example.\nAlso including newlines,\n\'quotes and \\backslashes'

If it's only the bytes over 0x80 you want to convert to \ooo:

printf %s "$string" |
  perl -l -0777 -pe 's/[\x80-\xff]/sprintf "\\%o", ord$&/ge'

would give (from any Bourne-like shell):

\344\277\235\345\255\230 = Save for example.
Also including newlines,
'quotes and \backslashes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.