Search code examples
linuxbashlocaleglibc

reading and writing from file


I have many questions about linux locales and especially different encodings. 1. Which locale is used when I perform the following command in bash?

// Привет - Russian "Hello"
echo "Привет" > ./test.txt 

As I understand it should use locale which is getting by locale command.

After some experiments of changing locale I always get correct answer on my console by cat ./test.txt. But why? Why when I change locale from "ru_RU.iso88595", "ru_RU.koi8r", etc. I always get correct result?

Does this mean that in reality my locale is not changed? Or file encoding is independent to the current locale?

Note: when I set LC_ALL by export LC_ALL="ru_RU.iso88595" I get bash: warning: setlocale: LC_ALL: cannot change locale (ru_RU.iso88595), but echo $? gives me 0 and later locale returns new locale. Is this locale really set?


Solution

  • Which locale is used when I perform the following command in bash?

    echo "Привет" > ./test.txt

    None. The data is written verbatim as it appears between the quotes. If the file is UTF-8, it will be written as UTF-8.

    Why when I change locale from "ru_RU.iso88595", "ru_RU.koi8r", etc. I always get correct result?

    cat somefile just dumps the file verbatim to your terminal. cat's and/or your shell's locale is not involved. The terminal shows it in whichever way it's been configured (and if you're using screen, it has its own input and output configuration separate from the shell and terminal).

    I get bash: warning: setlocale: LC_ALL: cannot change locale (ru_RU.iso88595)

    This is a message from Bash saying that it's having problems applying the new locale to the current session. You may have set the locale, but to an invalid value. Make sure it appears in the output of locale -a, otherwise it's not installed.