Skip to content

Instantly share code, notes, and snippets.

@noahlt
Last active September 10, 2017 18:34
Show Gist options
  • Save noahlt/9c0b368c09b7dcd32abcf7e646e9c3be to your computer and use it in GitHub Desktop.
Save noahlt/9c0b368c09b7dcd32abcf7e646e9c3be to your computer and use it in GitHub Desktop.
zsh Chinese yn prompt

The other day I saw this on Twitter:

zsh: 没想到吧,我还学了中文。

— Makito@nil (@sumimakito) September 8, 2017

Makito's Tweet says "This hadn't occurred to me, and I even know Chinese." In the screenshot, Oh My Zsh has given him a Y/n prompt asking whether he wants to enable automatic updates. He has responded by typing "成", which (in this context) means "OK!". Normally I would expect this sort of command-line prompt to assume anything that's not Y, y, YES, or yes to be a negative answer, but, lo and behold, in his screenshot Oh My Zsh understands his Chinese response and begins an update! Here's a plain text transcript:

[Oh My Zsh] Would you like to check for updates? [Y/n]: 成
Updating Oh My Zsh

When I tweeted about it, my friend Woonbin asked whether this was intentionally programmed behavior, or merely a coincidence. I decided to find out.

Github search led me to the relevant lines in the Oh My Zsh source:

if [[ "$line" == Y* ]] || [[ "$line" == y* ]] || [ -z "$line" ]; then
  _upgrade_zsh
else
  _update_zsh_update
fi

That code looks pretty straightforward, and accepts some surprising affirmitive responses like "Yep", "Yowza!", and "y followed by literally any string". There's no explicit handling for Chinese or any other language or character set. So at this point I thought Woonbin was right in suggesting it was a coincidence. I thought the "成" was being recognized by the [ -z "$line"] bit, which evaluates true if the string is empty, and is there to let the user hit the return key without typing to get the default option (proceeding with the update). I thought maybe zsh didn't handle Unicode characters well and thought a string with just Chinese in it was an empty string.

If that were the case, our line of code from Oh My Zsh would accept any Chinese string as an affirmative answer. With that hypothesis in mind, I copied the prompt into its own shell script to test out:

#/usr/bin/env zsh

echo "type something here to see whether zsh thinks it's a yes or no: \c"
read line

if [[ "$line" == Y* ]] || [[ "$line" == y* ]] || [ -z "$line" ]; then
    echo "yes"
else
    echo "no"
fi

Then I ran this script a few times to make sure it behaved the way it had been presented in the Tweet:

11:08:25 ~/tmp $ zsh test_zsh_chinese.sh
type something here to see whether zsh thinks it's a yes or no: y
yes
11:08:29 ~/tmp $ zsh test_zsh_chinese.sh
type something here to see whether zsh thinks it's a yes or no: n
no
11:08:31 ~/tmp $ zsh test_zsh_chinese.sh
type something here to see whether zsh thinks it's a yes or no: t
no
11:08:34 ~/tmp $ zsh test_zsh_chinese.sh
type something here to see whether zsh thinks it's a yes or no: 不
no
11:08:44 ~/tmp $ zsh test_zsh_chinese.sh
type something here to see whether zsh thinks it's a yes or no: 我
no
11:08:51 ~/tmp $ zsh test_zsh_chinese.sh
type something here to see whether zsh thinks it's a yes or no: 成
no

The three Chinese words I tested were 不 ("no"), 我 ("I"), and 成 (our original string, "OK!"). It turns out not only was my hypothesis wrong, but the test with 成 shows that this script doesn't even match the behavior reported in the original Tweet with the screenshot!

I went back to the Tweet and read the replies to it. Most of the replies say that their expectation matched mine—that anything without a "y" would be assumed to be a negative answer. One person suggests that since the prompt says "Y/n", it shouldn't accept other answers. Another suggests that the original Tweet author should submit a pull request to Oh My Zsh.

Oh, yeah, and another person says they still prefer bash to zsh.

Anyways, it turns out I didn't learn much.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment