Linux: Set language to default for console or scripts

Sometimes you may want to change the output language of commands. E.g. if you have to google for error messages or need the output for further use in scripts.

As seen in this post the output of commands differs depending on the configured system language.

E.g. the command

lscpu

on a German installation looks  like this:

Architektur: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte-Reihenfolge: Little Endian
CPU(s): 2
On-line CPU(s) list: 0,1
Thread(s) pro Kern: 1
Kern(e) pro Socket: 2
Socket(s): 1
Anbieterkennung: GenuineIntel
Prozessorfamilie: 6
Modell: 23
Modellname: Intel(R) Core(TM)2 Duo CPU E7400 @ 2.80GHz
Stepping: 10
CPU MHz: 2800.000
Maximale Taktfrequenz der CPU:2800,0000
Minimale Taktfrequenz der CPU:1603,0000
BogoMIPS: 5487.43
L1d Cache: 32K
L1i Cache: 32K
L2 Cache: 3072K

The same command on an English installation looks like this:

Architecture:          x86_64
CPU op-mode(s):        32-bit, 64-bit
Byte Order:            Little Endian
CPU(s):                2
On-line CPU(s) list:   0,1
Thread(s) per core:    1
Core(s) per socket:    2
Socket(s):             1
Vendor ID:             GenuineIntel
CPU family:            6
Model:                 23
Model name:            Intel(R) Core(TM)2 Duo CPU     E7400  @ 2.80GHz
Stepping:              10
CPU MHz:               2800.000
CPU max MHz:           2800.0000
CPU min MHz:           1603.0000
BogoMIPS:              5487.43
L1d cache:             32K
L1i cache:             32K
L2 cache:              3072K

Now, imagine you want to use the “CPU max MHz” value in a script for some reason. Depending on the system language you would have to look for different keywords (“Maximale Taktfrequenz der CPU” vs. “CPU max MHz”) and would have treat the value in different ways (as in German the decimal point is , instead of .).
So, what’s the solution for this? To get the output of a command in the default language (which is English in most cases) just use LC_ALL=C as a prefix. E.g.:

LC_ALL=C lscpu

This gives the output in English on all linux systems.

If you want to set the default language for the whole terminal session or script use:

export LC_ALL=C

Regarding scripts that rely on output of linux commands it is good practice to do an export LC_ALL=C at the beginning of a script. This way you are able to parse command output the same way regardless of system language settings.

Tagged with: , , , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*