EGS EGS - 1 year ago 48
Bash Question

Language in the output of a shell script is changing

I wrote a script that lists all the packages installed on a machine, and then compares it with another list located in

. For some reason, the language of the output changes sometimes. It alternates between English and Portuguese.

The first time I run the script, it outputs
(Monday), but in the second run the date is
, which is the diminutive for Segunda (Monday in Portuguese).

Also, when in English, the words are getting cutted at the 14º character.

This is problematic because totaly unvalidates the comparison.

Below is an excerpt of the script that creates the list and performs the comparison:

dpkg -l | awk '{print $2}' >> /tmp/lista_pacotes.log

echo "\nPackages added until" $(date) "\n" >> /tmp/diferencas.log

grep -F -x -v -f /tmp/pacotes_iniciais.log /tmp/lista_pacotes.log >> /tmp/diferencas.log

The content of

Second run:

Packages added until Seg Nov 14 08:48:22 BRT 2016


First run:

Packages added until Mon Nov 14 08:24:17 BRT 2016



What could be causing this?

Note: I am running this script on a 64-bit Debian Jessie OS, and accessing this machine through SSH.

Answer Source

From the info page for date:

Invoking ‘date’ with no FORMAT argument is equivalent to invoking it with a default format that depends on the ‘LC_TIME’ locale category.


LC_TIME=en_US.UTF-8 date
LC_TIME=ru_RU.UTF-8 date

Sample Output

Mon Nov 14 20:27:59 +07 2016
Пн ноя 14 20:28:04 +07 2016

So the behavior you described is due to your locale settings, namely the LC_TIME environment variable. Adjust it somewhere before running the script (on the target machine!), e.g.:

export LC_TIME=C

The other issues are most likely due to the locale settings as well. So adjust the rest of the locale environment variables in similar manner. LANG and LC_NUMERIC variables are particularly important. Check out the info page: info bash LANG.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download