如何更改shell脚本字符编码?

时间:2021-05-29 20:38:14

I am using Gina Trapiani's excellent todo.sh to organize my todo-list.

我正在使用Gina Trapiani的优秀todo.sh来组织我的待办事项列表。

However being a dane, it would be nice if the script accepted special danish characters like ø and æ.

然而,作为一个丹麦人,如果剧本接受像ø和æ这样的特殊丹麦人物,那就太好了。

I am an absolute UNIX-n00b, so it would be a great help if anybody could tell me how to fix this! :)

我是一个绝对的UNIX-n00b,所以如果有人能告诉我如何解决这个问题,那将是一个很大的帮助! :)

2 个解决方案

#1


14  

What does this command show?

这个命令显示了什么?

locale

It should show something like this for you:

它应该为你显示这样的东西:

LC_CTYPE="da_DK.UTF-8"
LC_NUMERIC="da_DK.UTF-8"
LC_TIME="da_DK.UTF-8"
LC_COLLATE="da_DK.UTF-8"
LC_MONETARY="da_DK.UTF-8"
LC_MESSAGES="da_DK.UTF-8"
LC_PAPER="da_DK.UTF-8"
LC_NAME="da_DK.UTF-8"
LC_ADDRESS="da_DK.UTF-8"
LC_TELEPHONE="da_DK.UTF-8"
LC_MEASUREMENT="da_DK.UTF-8"
LC_IDENTIFICATION="da_DK.UTF-8"
LC_ALL=

If not, you might try doing this before you run your script:

如果没有,您可以在运行脚本之前尝试执行此操作:

LANG=da_DK.UTF-8

You don't say what happens when you run the script and it encounters these characters. Are they in the todo file? Are they entered at a prompt? Is there an error message? Is something output in place of the expected output?

您没有说明运行脚本时会发生什么,并且遇到这些字符。它们是否在todo文件中?他们是在提示时输入的吗?有错误信息吗?输出是否取代了预期的输出?

Try this and see what you get:

试试这个,看看你得到了什么:

read -p "Enter some characters" string
echo "$string"

#2


15  

Slowly, the Unix world is moving from ASCII and other regional encodings to UTF-8. You need to be running a UTF terminal, such as a modern xterm or putty.

慢慢地,Unix世界正在从ASCII和其他区域编码转向UTF-8。您需要运行UTF终端,例如现代xterm或putty。

In your ~/.bash_profile set you language to be one of the UTF-8 variants.

在〜/ .bash_profile中,您将语言设置为UTF-8变体之一。

export LANG=C.UTF-8
or
export LANG=en_AU.UTF-8
etc..

You should then be able to write UTF-8 characters in the terminal, and include them in bash scripts.

然后,您应该能够在终端中编写UTF-8字符,并将它们包含在bash脚本中。

#!/bin/bash
echo "UTF-8 is græat ☺"

See also: https://serverfault.com/questions/11015/utf-8-and-shell-scripts

另见:https://serverfault.com/questions/11015/utf-8-and-shell-scripts

#1


14  

What does this command show?

这个命令显示了什么?

locale

It should show something like this for you:

它应该为你显示这样的东西:

LC_CTYPE="da_DK.UTF-8"
LC_NUMERIC="da_DK.UTF-8"
LC_TIME="da_DK.UTF-8"
LC_COLLATE="da_DK.UTF-8"
LC_MONETARY="da_DK.UTF-8"
LC_MESSAGES="da_DK.UTF-8"
LC_PAPER="da_DK.UTF-8"
LC_NAME="da_DK.UTF-8"
LC_ADDRESS="da_DK.UTF-8"
LC_TELEPHONE="da_DK.UTF-8"
LC_MEASUREMENT="da_DK.UTF-8"
LC_IDENTIFICATION="da_DK.UTF-8"
LC_ALL=

If not, you might try doing this before you run your script:

如果没有,您可以在运行脚本之前尝试执行此操作:

LANG=da_DK.UTF-8

You don't say what happens when you run the script and it encounters these characters. Are they in the todo file? Are they entered at a prompt? Is there an error message? Is something output in place of the expected output?

您没有说明运行脚本时会发生什么,并且遇到这些字符。它们是否在todo文件中?他们是在提示时输入的吗?有错误信息吗?输出是否取代了预期的输出?

Try this and see what you get:

试试这个,看看你得到了什么:

read -p "Enter some characters" string
echo "$string"

#2


15  

Slowly, the Unix world is moving from ASCII and other regional encodings to UTF-8. You need to be running a UTF terminal, such as a modern xterm or putty.

慢慢地,Unix世界正在从ASCII和其他区域编码转向UTF-8。您需要运行UTF终端,例如现代xterm或putty。

In your ~/.bash_profile set you language to be one of the UTF-8 variants.

在〜/ .bash_profile中,您将语言设置为UTF-8变体之一。

export LANG=C.UTF-8
or
export LANG=en_AU.UTF-8
etc..

You should then be able to write UTF-8 characters in the terminal, and include them in bash scripts.

然后,您应该能够在终端中编写UTF-8字符,并将它们包含在bash脚本中。

#!/bin/bash
echo "UTF-8 is græat ☺"

See also: https://serverfault.com/questions/11015/utf-8-and-shell-scripts

另见:https://serverfault.com/questions/11015/utf-8-and-shell-scripts