Correctly setting environment

I’m using zsh and gdm to run gnome. Some time ago I discovered that variables are not set correctly. For example LANG/LC_ALL are incorrect ("" instead of en_GB.UTF-8).

I split the .zshrc into .zshrc and .profile. In the latter I set the environment variables, but how can I set the variables before the session starts? I tried a few choices (.xinitrc, .xsessionrc) but none seemed to work.

Edit To clarify – I used .profile and manually sourced it in .zshrc. It does not change question anyway.

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

The simple way is to invent a time machine, visit the various people who devised shell startup files and tell them to cleanly distinguish between three things:

  • session setup, e.g. environment variables;
  • session launching, i.e., e.g. starting a command-line shell or a window manager or running startx;
  • shell initialization, e.g. aliases, prompt, key bindings.

It’s not too hard to get session vs. shell right in a portable way: login-time initialization goes into .profile (or .zprofile, or .login), shell initialization goes in .bashrc or .zshrc. I’ve previously written about .bash_profile, zsh vs. other shells, more about portability (mostly about bash), more about who reads .profile.

A remaining problem is distinguishing between session setup and session launching. In most cases, ~/.profile is executed when you log in and can double as both, but there are exceptions:

  • If your login shell is (t)csh or zsh, ~/.login and ~/.zprofile is sourced instead of ~/.profile. Ditto for bash and ~/.bash_profile, but this is easily solved by sourcing ~/.profile from ~/.bash_profile.
  • If you log in under a display manager (xdm, gdm, kdm, …), whether your ~/.profile is read depends on the version of the program, on your distribution (Linux or otherwise), and on what session type you choose.
    • If you count on the display manager to start a session for you, your .profile must set environment variables but not start a session (e.g. a window manager).
    • The traditional configuration file for X sessions is ~/.xsession, doing both session setup and session launching. So the file can be essentially . ~/.xsession; . ~/.xinitrc. Some distributions source ~/.profile before ~/.xsession. Modern distributions only source ~/.xsession when you select a “custom” session from the display manager, and such a session is not always available.
    • Your session manager may have its own way of setting environment variables. (That’s an optional part of your desktop environment, chosen by you through a configuration file or by selecting a session type when logging in; don’t confuse it with the session startup scripts provided by the display manager, which are executed under your user but chosen on a system-wide basis. Yes, it’s a mess.)

In summary, ~/.profile is the right place for environment variables. If it’s not read, try sourcing it from ~/.xsession (and start your X programs from there), or look for a system-specific method (which may depend on your distribution, display manager if any, session type if display manager, and desktop environment or session manager).

Method 2

In Gentoo documentation there is article Gentoo Linux Localization Guide. Chapter 3 is about setting locale.

Most typically users only set the LANG variable on the global basis. This example is for a unicode German locale:

Code Listing 3.1: Setting the default system locale in /etc/env.d/02locale

LANG="en_GB.UTF-8"

In my Gentoo LANG is set in this file and everything is working properly…

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="deaebcb39eaabfabacb7">[email protected]</a> ~ $ cat /etc/env.d/02locale 
LANG="pl_PL.UTF-8"

<a href="https://getridbug.com/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="8cfceee1ccf8edf9fee5">[email protected]</a> ~ $ locale
LANG=pl_PL.UTF-8
LC_CTYPE="pl_PL.UTF-8"
LC_NUMERIC="pl_PL.UTF-8"
LC_TIME="pl_PL.UTF-8"
LC_COLLATE="pl_PL.UTF-8"
LC_MONETARY="pl_PL.UTF-8"
LC_MESSAGES="pl_PL.UTF-8"
LC_PAPER="pl_PL.UTF-8"
LC_NAME="pl_PL.UTF-8"
LC_ADDRESS="pl_PL.UTF-8"
LC_TELEPHONE="pl_PL.UTF-8"
LC_MEASUREMENT="pl_PL.UTF-8"
LC_IDENTIFICATION="pl_PL.UTF-8"
LC_ALL=


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x