Temporarily change language for terminal messages/warnings/errors
Solution 1
There are several environment variables available for changing language settings. You can view your current locale settings by executing the locale
command. To change all locale settings to English, use LANG=C
. This C
locale is always available without installing additional language packs. (In order to temporarily change to non-English locales, see @mklement0's post.)
Examples:
Executing a command with the default language settings and print the current locale settings:
$ /nonexistent
bash: /nonexistent: Bestand of map bestaat niet
$ locale
LANG=nl_NL.UTF-8
LANGUAGE=
LC_CTYPE="nl_NL.UTF-8"
LC_NUMERIC="nl_NL.UTF-8"
LC_TIME="nl_NL.UTF-8"
LC_COLLATE="nl_NL.UTF-8"
LC_MONETARY="nl_NL.UTF-8"
LC_MESSAGES="nl_NL.UTF-8"
LC_PAPER="nl_NL.UTF-8"
LC_NAME="nl_NL.UTF-8"
LC_ADDRESS="nl_NL.UTF-8"
LC_TELEPHONE="nl_NL.UTF-8"
LC_MEASUREMENT="nl_NL.UTF-8"
LC_IDENTIFICATION="nl_NL.UTF-8"
LC_ALL=
Temporarily override the language for one program and show that it is really temporary:
$ LANG=C ls /nonexistent
ls: cannot access /nonexistent: No such file or directory
$ ls /nonexistent
ls: kan geen toegang krijgen tot /nonexistent: Bestand of map bestaat niet
Change the locale for all commands executed in the current shell and include proofs again:
$ LANG=C
$ ls /nonexistent
ls: cannot access /nonexistent: No such file or directory
$ locale
LANG=C
LANGUAGE=
LC_CTYPE="C"
LC_NUMERIC="C"
LC_TIME="C"
LC_COLLATE="C"
LC_MONETARY="C"
LC_MESSAGES="C"
LC_PAPER="C"
LC_NAME="C"
LC_ADDRESS="C"
LC_TELEPHONE="C"
LC_MEASUREMENT="C"
LC_IDENTIFICATION="C"
LC_ALL=
Solution 2
Lekensteyn's helpful answer works great if you want to switch to US English on demand, as the OP requested, but if you want to switch to a different language on demand, more work is needed.
Before starting, you must install message tables with sudo apt-get install language-pack-<lang-tag>
, where <lang-tag>
is a simple RTF 5646 language subtag, such as es
for Spanish.
Background info
GNU gettext-based utilities give precedence to the nonstandard LANGUAGE
environment variable[1]
over POSIX-defined locale environment variables LC_ALL
, LC_MESSAGES
, and LANG
(in that order).
Given that LANGUAGE
is set by default on Ubuntu systems[2], namely to a substring of the LANG
value that reflects either a simple language tag (e.g., es
for Spanish) or a language-region tag (e.g., de_DE
for the Germany variant of German), you must unset or override LANGUAGE
in order for a different language's messages to take effect.[3]
Option 1: Set LANGUAGE
Example: Switch to Spanish (es
) messages ad-hoc:
$ LANGUAGE=es ls NoSuchFile
ls: no se puede acceder a NoSuchFile: No existe el archivo o el directorio
Note: A simple language tag such as es
is sufficient, but you may add a region identifier (e.g., es_AR
for Argentina), and even a charset suffix (e.g., es_AR.UTF-8
).
However, localized messages may only exist at the language level, and the fallback is to use messages that match the language part (es
, in this case).
Option 2: Unset LANGUAGE
and set LC_ALL
This alternative solution undefines LANGUAGE
first, and then uses POSIX locale environment variable LC_ALL
to implicitly set LC_MESSAGES
[4]:
$ LANGUAGE= LC_ALL=es_ES.UTF-8 ls NoSuchFile
ls: no se puede acceder a NoSuchFile: No existe el archivo o el directorio
This solution has the advantage of setting all localization aspects to the specified locale (such as LC_TIME
for date/time formats) and by (implicitly) setting LC_MESSAGES
also informs non-GNU programs of the desired language.
Note how LC_ALL
requires the exact, full locale name, including charset suffix, to be effective (es_ES.UTF-8
) (unlike LANGUAGE
, for which a simple language tag is sufficient (like es
)). The same applies to setting LC_MESSSAGES
and LANG
. Specifying an invalid / non-installed locale name causes fallback to the POSIX locale and therefore US English.
Footnotes
[1] The reasons that Lekensteyn's answer works even _without_ unsetting / overriding `LANGUAGE` is an _exception_: if the (effective) `LC_MESSAGES` value (typically set indirectly via `LANG` or `LC_ALL`) is either `C` or (its synonym) `POSIX`, that value is respected, irrespective of the value of `LANGUAGE`, if any. Conversely, if the (effective) `LC_MESSAGES` value is any other, _specific_ locale, `LANGUAGE` takes precedence. [2] This applies to [Ubuntu proper](https://www.ubuntu.com/), but not necessarily to [other flavors](https://www.ubuntu.com/about/about-ubuntu/flavours); Lekensteyn states that [Kubuntu](https://kubuntu.org/) does _not_ set `LANGUAGE`. Arguably, `LANGUAGE` should _not_ be set by default, given that in its absence the `LC_MESSAGES` value implied by the `LANG` value (which determines the current locale), is respected. [3] You can also use this approach to switch to [US] English by assigning either `LANGUAGE=C` or `LANGUAGE=POSIX` (as an alternative to, `LANG=C` / `LANG=POSIX`), though I'm unclear whether that is actively recognized or simply a _fallback_ mechanism, given that these values don't start with a _language_ tag; perhaps the better choice would be `en_US`. [4] There's an _edge_ case where this approach doesn't work: Trying to invoke an executable with a _path_ - whether relative or absolute - does NOT switch to the specified language, whereas a _mere filename_ does: `LANGUAGE= LC_ALL=es_ES.UTF-8 /path/to/no_such_utility` does _not_ work (outputs a message in the current locale), whereas `LANGUAGE= LC_ALL=es_ES.UTF-8 no_such_utility` does (outputs a Spanish error message). If anyone knows why and whether there's a good reason for this, do let us know.Related videos on Youtube
takeshin
Updated on September 18, 2022Comments
-
takeshin almost 2 years
Messages in my terminal are displayed using Russian language by default, which is my native one.
For just a moment I want them to be in English (eg. to paste in forums), then switch back to the default language.
How can I do the switch and switch back using bash?
-
gertvdijk about 11 yearsFor other users having issues getting this to work - setting
LANG
orLANG_ALL
isn't working for me, yetLANGUAGE
is. See Why is overriding the LANG environment variable not changing the language for me? -
mklement0 over 7 years@gertvdijk: Thanks for that; the reason this answer works even without setting
LANGUAGE
is an exception: GNU gettext gives precedence to theLANGUAGE
value except if the (effective)LC_MESSAGES
value (typically set indirectly viaLANG
orLC_ALL
) is eitherC
or (its synonym)POSIX
. Also note thatLANGUAGE
happens to be unset in this answer, whereas it is set by default, and if it is set, you must override it to switch to a specific locale's language (as opposed to "C" / "POSIX"), which is what you found. -
mklement0 over 7 yearsYour answer works great when switching to the "C" locale (with US English messages, as requsted), but won't work for other locales unless
LANGUAGE
is explicitly unset or overridden. Given the question's generic title, it is likely that people will find this answer looking to switch to a non-English language also, so please consider adding this information to your answer. -
Lekensteyn over 7 years@mklement0 Given the context of the question (posting English error messages), I think that the current post is sufficient. You can add another answer to explain the details around
LANGUAGE
if you want :) -
mklement0 over 7 yearsI've added my own answer, as you suggested. If you agree with my assessment that future readers may come here looking to switch to any language (as several people have already actively indicated), please add a link to my answer to your question. Aside from that, I suggest changing the value of
LANGUAGE
in your sample output tonl
, which is the actual default value when your locale isnl_NL.UTF-8
. -
Lekensteyn over 7 years@mklement0 The output from
locale
on a Kubuntu 16.04 desktop have emptyLANGUAGE
andLC_ALL
while the others containnl_NL.UTF-8
. I've updated the answer to link to the non-English scenario. (btw, you might want to use less bold-faced text in your post, it is somehow makes it harder to read.) -
mklement0 over 7 years@Lekensteyn: I appreciate that you've added a link. Good to know that other Ubuntu flavors are configured differently - I've added a note to my answer. As an aside: Arguably, not setting
LANGUAGE
by default makes more sense. ReLC_ALL
: I don't think any Unix-like OS setsLC_ALL
by default, as that would override the indiv.LC_*
variables (rather than set as an overridable default, asLANG
does). Point taken re bolding - I've toned it down. -
mklement0 over 7 yearsTip of the hat to @wjandrea for his help with structuring this post.