Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!

How use Unicode characters in shell scripts UNIX
Sometimes when you type a command in Terminal, you need to enter Unicode characters in a parameter string, for example with the defaults command. If you type them in directly (with some Option-key shortcut or using the Character Palette), a sequence of numbers and backslashes will be displayed (the octal notation for the UTF8 encoding of the Unicode character). Often this works OK directly on the command line, but not from within a shell script.

A way to solve this problem is to use the form $'string' (in a bash script). For example, to enter the ellipsis character in the definition of a menu shortcut:
$ defaults write com.apple.iTunes NSUserKeyEquivalents \
$'Subscribe to Podcast342200246' "@S"
Or to enter up and down arrows:
$ defaults write com.apple.Safari NSUserKeyEquivalents \
-dict-add "Select Previous Tab" $'@342206223' # Option+Up
$ defaults write com.apple.Safari NSUserKeyEquivalents \
-dict-add "Select Next Tab" $'@342206221' # Option+Down
I don't understand what causes the different behaviour in shell scripts, but this seems to work both on the command line and in shell scripts. I think this also answers Rob's unanswered question at the end of the Fix iChat's iTunes status message format hint. The string 342231253 in that hint is the octal notation for the UTF8 encoding of Unicode character U266B: "Beamed eighth notes."
    •    
  • Currently 1.00 / 5
  • 1
  • 2
  • 3
  • 4
  • 5
  (1 vote cast)
 
[9,903 views]  

How use Unicode characters in shell scripts | 4 comments | Create New Account
Click here to return to the 'How use Unicode characters in shell scripts' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
How use Unicode characters in shell scripts
Authored by: boredzo on Jan 09, '06 12:09:26PM

I really wish GeekLog displayed backslash escapes correctly…



[ Reply to This | # ]
How use Unicode characters in shell scripts
Authored by: mingo on Jan 09, '06 05:53:02PM

This post reminds that I have wondered how to get Terminal and tcsh to accept accented characters. e.g. option-e e, for a e-acute? I have played around with Window Settings/Display/Character Set Encoding in the Terminal Inspector but this did not appear to have an effect.



[ Reply to This | # ]
How use Unicode characters in shell scripts
Authored by: david-bo on Jan 10, '06 03:43:03PM

You need a newer version of tcsh. The version included in 10.4 does not (have good) support (for) accented characters.

---
http://www.google.com/search?as_q=%22Authored+by%3A+david-bo%22&num=10&hl=en&ie=ISO-8859-1&btnG=



[ Reply to This | # ]
How use Unicode characters in shell scripts
Authored by: Christoph on Jan 11, '06 04:53:25AM
I had the same problem with bash. As it uses the readline library the solution were the following entries in the readline configuration file $HOME/.inputrc:

set input-meta on
set output-meta on
set convert-meta off

Other useful settings are

# suppress bell ringing
set bell-style none

# case insensitive TAB-completion
set completion-ignore-case on

# enable Delete/Home/end key in xterm and iTerm 
# (does not work in Terminal.app though)
"\e[3~": delete-char 
"\e[1~": beginning-of-line
"\e[4~": end-of-line


[ Reply to This | # ]