The answer to your specific question is that often only /etc/bash.bashrc
(or /etc/bashrc
) is loaded.
There seem to be a couple of ways to address this, most are workarounds unfortunately. In no particular order of preference:
Fake a login
Logins run /etc/profile
and often you can place your custom scripts in /etc/profile.d/*.sh
Wrap it in a bash login
Sometimes it is easiest to trigger a login, i.e.
echo "do whatever $PATH $BASH $PS1 $0"
becomes
bash -lc 'echo "do whatever $PATH $BASH $PS1 $0"'
Manually load the profile
Do what logins do, manually load the profile
source /etc/profile;echo "do whatever $PATH $BASH $PS1 $0"
Edit global environment variables
For simple variables you can add to /etc/environment
see Ubuntu System-wide_environment_variables but variable expansion does not work
THING_HOME="/opt/thing"
PATH="$PATH:/opt/thing" # this will NOT work
For PATH you could append to it e.g. sudo sed -i 's#PATH=\"[^\"]*#&:/opt/thing#' /etc/environment
Edit all bash shells
You can add to /etc/bash.bashrc
(or /etc/bashrc
) however this normally exits early if non-interactive so you need to hack your additions on before:
# If not running interactively, don't do anything
[ -z "$PS1" ] && return
Do none of these
- Consider symlinks if you are trying to add something to the PATH
- Consider making your programs/scripts modify their own environment
- Systemd has
EnvironmentFile
and Environment
e.g. systemctl edit --full cron.service
I am interested in hearing if anyone else has better workarounds!
Some processes create their own environment such as
cron
andsudo
. – fd0 – 2017-05-03T16:39:03.737And I don't know if this is limited to Ubuntu, but Ubuntu has system-wide environment variable settings: https://help.ubuntu.com/community/EnvironmentVariables#System-wide_environment_variables , which mentions
– vol7ron – 2017-05-03T17:43:22.223sudo
. For instance, I would expectcron
to have$USER
already set, but instead it sets$LOGNAME
-- I think (can't remember exactly)