I'm trying to set environment variables for non-interactive non-login shell. I know bash reads the contents of ~/.bashrc before execute the command. In the beginning of the script there's a part:
*# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac*
So I think if I add something above it, it will take effect no matter if the shell is interactive or not:
export VAR=something
# If not running interactively, don't do anything
case $- in
*i*) ;;
*) return;;
esac
However it doesn't work :(. I want to avoid using $BASH_ENV because it messes up my xkb settings. I remapped some keys in /usr/share/X11/xkb/symbols/pc. And if I set $BASH_ENV, it will just loads the default keymap.
In general, $HOME/. bashrc is executed for non-interactive login shells, but no script can be guaranteed to run for a non-interactive non-login shell. You can force it by setting (and exporting!) BASH_ENV from a parent shell to the name of a script which you want to execute when a non-interactive shell is started.
Most Common Environment VariablesHOME – The user's home directory location. SHELL – Current shell (bash, zsh, etc.). LOGNAME – Name of the user.
Shell variables are only present in the shell in which they were defined. Environment variables are inherited by child shells but shell variables are not. Shell variable can be made an environment variable by using export command. A script is simply a collection of commands that are intended to run as a group.
Solution for Ubuntu: set the variables in /etc/environment
, and it works for all users and all types of shells.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With