You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Linux修改LANG后阿拉伯字符仍显示为????的问题求助

Fixing Arabic Character Encoding Issues in KSH Database Export Scripts

Hey Sameer, I’ve run into similar encoding headaches with non-Latin characters in shell scripts before—let’s break down the missing pieces that might be causing those ???? replacements for your Arabic text. Even though you’ve set LANG=ar_SA.utf8, there are several other critical places to check:

1. Ensure Database Client Encoding Matches the Server

Most databases require explicit encoding configuration for client connections, separate from the system’s LANG variable:

  • Oracle: Set the NLS_LANG environment variable to match your database’s character set (e.g., for UTF-8):
    export NLS_LANG=ARABIC_SAUDI_ARABIA.AL32UTF8
    
  • MySQL/MariaDB: Add encoding flags to your database command or set the client variable:
    mysql --default-character-set=utf8mb4 -u your_user -p your_db -e "SELECT ..." > output.txt
    
  • PostgreSQL: Use the client_encoding parameter in your query or connection string:
    psql -d your_db -c "SET client_encoding = 'UTF8'; SELECT ..." > output.txt
    

2. Explicitly Set All Locale Variables in Your KSH Script

Just setting LANG might not be enough—some tools prioritize LC_CTYPE or LC_ALL for character handling. Add these lines at the top of your script to enforce consistent UTF-8:

export LANG=ar_SA.utf8
export LC_ALL=ar_SA.utf8
export LC_CTYPE=ar_SA.utf8

LC_ALL acts as an override for all other locale settings, so this ensures no conflicting variables are messing with encoding.

3. Verify Your Export Tool Writes UTF-8

Some command-line tools default to system legacy encodings unless told otherwise. For example:

  • If using sqlplus, add these lines before your query to force UTF-8 output:
    SET SERVEROUTPUT ON
    SET NAMES UTF8
    SET TERMOUT OFF
    
  • If redirecting output directly (e.g., your_query > output.txt), ensure the shell doesn’t perform unintended encoding conversion. You can confirm the output encoding with:
    file output.txt
    
    The result should show something like output.txt: UTF-8 Unicode text.

4. Check Database Server’s Character Set

Double-check that your database itself is storing Arabic characters in a UTF-8 compatible set (like AL32UTF8 for Oracle, utf8mb4 for MySQL). If the server is using a legacy encoding (e.g., ISO-8859-6), even correct client settings won’t fix the export—you’d need to migrate the data to a UTF-8 collation first.

Quick Test to Isolate the Issue

Run a simple query directly in your shell (not via script) with the same environment variables, then check the output:

export LANG=ar_SA.utf8
export NLS_LANG=ARABIC_SAUDI_ARABIA.AL32UTF8 # Adjust for your DB
sqlplus your_user/your_pass@your_db <<EOF
SELECT arabic_column FROM your_table WHERE rownum <= 1;
EOF

If this shows correct Arabic text, the problem is in your script’s environment variable inheritance—make sure the script isn’t overriding LANG or NLS_LANG somewhere.

内容的提问来源于stack exchange,提问作者Sameer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.29 08:08:28