#580587 bash: Please encourage useful variable setting

Package:
bash
Source:
bash
Description:
GNU Bourne Again SHell
Submitter:
Reuben Thomas
Date:
2010-05-06 23:18:04 UTC
Severity:
wishlist
#580587#5
Date:
2010-05-06 23:14:22 UTC
From:
To:
There's nothing in /etc/skel/.profile about setting environment
variables. Now that dash is /bin/sh, it's worth trying to do this nicely.

I make the following suggestion; I welcome improvements.

In my ~/.profile, I have at the top:

# Specify environment
ENV=$HOME/.shvars; export ENV
BASH_ENV=$ENV; export BASH_ENV

and at the bottom:

# Load environment variables
if [ -n "$ENV" ]; then . "$ENV"; fi

The placing of these lines is so far fairly arbitrary; the only
important point in my setup is that the sourcing of $ENV is done at
the bottom because my .shvars modifies $PATH. Proper treatment of this
depends on a fix for bug #379696, which I don't presume to address
here.

These changes achieve the following thing (with a bit more
documentation, which I'd be happy to write if the changes were
adopted):

1. Give the user an obvious place to set environment variables. I
   would also add /etc/skel/.shvars, containing some comments pointing
   out that you need to use POSIX syntax, i.e. 'FOO="bar"; export FOO'
   rather than 'export FOO="bar"'.

2. Make the environment variables available to all /bin/sh and
   /bin/bash scripts (via BASH_ENV and ENV).

The main problem I foresee is that users might want different sets of
variables available in interactive from non-interactive sessions.
Hence, it might be better to recommend two variables files, say
~/.shvars and ~/.shvars_interactive, where ~/.shvars_interactive
includes and overrides ~/.shvars (this way round seems more useful to
me than the other way around, though either would work) and set ENV
and BASH_ENV to ~/.shvars as above, but source ~/.shvars_interactive
instead. But maybe this is an unnecessary complexity. (I certainly
don't have a need for it at present.)

I just spent some time working all this out to get environment
variables working nicely in GNOME without having manually to load an
environment anywhere, so I suspect that something of this sort could
save other users time, as well as promoting good practice.