skills/modelscope.cn/chinese-script-encoding

chinese-script-encoding

Installation
SKILL.md

跨平台中文脚本编码规范

在 Windows、Linux/macOS、鸿蒙(OpenHarmony)等系统上生成含中文的脚本时,核心原则是让文件编码与解释器的默认解码一致。本规范覆盖主流脚本类型,目标是"既能用中文,又不乱码导致执行失败"。

触发条件

  • 用户要求生成/编写脚本(.ps1 / .bat / .vbs / .sh / .bash / .zsh / .ets / .ts / .js / .py)且包含中文(注释、字符串、路径、输出)。
  • 用户报告脚本因中文乱码执行失败:SyntaxError: Non-UTF-8 code、PowerShell/cmd 中文乱码、shell 打不开、TS/JS 编译报错等。

按系统分类的推荐编码

系统/平台 脚本扩展名 推荐保存编码 关键措施
Windows .ps1 UTF-8 带 BOM (utf-8-sig) 关键是文件编码;运行前可设 [Console]::OutputEncoding = [System.Text.Encoding]::UTF8
Windows .bat / .cmd GBK/ANSI,或 UTF-8 带 BOM + 首行 chcp 65001 >nul 二选一;路径避免中文
Windows .vbs GBK/ANSI,或 UTF-16LE 带 BOM Windows Script Host 默认 ANSI;UTF-8 无 BOM 会乱码
Linux / macOS .sh / .bash / .zsh UTF-8 无 BOM LF 换行;chmod +x;shebang 如 #!/usr/bin/env bash
鸿蒙(OpenHarmony 底层) .sh UTF-8 无 BOM 开发者权限下终端可用;同 Linux shell 规范
鸿蒙(应用/生态层) .ets / .ts / .js UTF-8 无 BOM ArkTS/TS/JS 编译器默认 UTF-8;LF 换行;路径建议英文
跨平台 .py UTF-8(带 BOM 也兼容) 头部 # -*- coding: utf-8 -*-;读写显式 encoding='utf-8'
Installs
1
First Seen
11 days ago