質問

What is the proper way of inserting a copyright symbol © and a pound sterling symbol £ into a bash script.

I am using nano as my editor and am running Debian Squeeze.

If I copy and paste the symbol from windows, it works but it seems to paste hidden characters that nano cannot display and it makes it hard to edit code in the shell script then.

So for example I want to be able to do this:-

text="£15.00"
text2="© John Doe"
役に立ちましたか?

解決

It seems you are using a locale that doesn't support these symbols (in the olden days it could have also been a bad font). Since you are using UTF-8 characters (default on windows), you also need to be in a UTF-8 aware environment on linux. You can check that with the locale command:

$ locale
LANG=en_US.UTF-8
LC_CTYPE="en_US.UTF-8"
LC_NUMERIC="en_US.UTF-8"
LC_TIME="en_US.UTF-8"
LC_COLLATE="en_US.UTF-8"
LC_MONETARY="en_US.UTF-8"
LC_MESSAGES="en_US.UTF-8"
LC_PAPER="en_US.UTF-8"
LC_NAME="en_US.UTF-8"
LC_ADDRESS="en_US.UTF-8"
LC_TELEPHONE="en_US.UTF-8"
LC_MEASUREMENT="en_US.UTF-8"
LC_IDENTIFICATION="en_US.UTF-8"
LC_ALL=

So my system is fine, but yours had the default, ASCII C locale set. These are simple bash variables, so you could export them individually or override them all (by setting LC_ALL). However, that would only affect the current shell, so you need to make it part of shell initialisation to work everytime automatically.

This is usually done through profile scripts in /etc globally, but .bash_profile or .bashrc would work just as well. In your version of Debian the locale is stored in /etc/default/locale and you can get the list of available choices with locale -a, eg.:

$ locale -a
C
en_US
en_US.iso88591
en_US.utf8
POSIX
sl_SI.utf8

Pick one with an utf8 suffix. If you need other locales (for example japanese), they can be generated with localedef/locale-gen (comes with glibc or as a separate package).

他のヒント

If you're having problems with entering the symbols in nano, use escape syntax. You can use iconv to convert it to the output encoding corresponding to the current locale:

text=$'\xa315.00'
text2=$'\xa9 John Doe'
iconv -f latin1 <<< $text
iconv -f latin1 <<< $text2

or the same in utf8:

text=$'\xc2\xa315.00'
text2=$'\xc2\xa9 John Doe'
iconv -f utf-8 <<< $text
iconv -f utf-8 <<< $text2

Better just use (C) for copyright, you want it to be always readable in other locales and not a defense for violation.

The display looks like

text="1.00"
text2=" ohn Doe"

in my test terminal.

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top