Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Set environment variables for non-interactive shell

Tags:

linux

bash

shell

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.

like image 561
Trung Quan Vo Avatar asked Mar 10 '17 18:03

Trung Quan Vo


People also ask

How do I set an environment variable in a non-interactive shell?

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.

What are 3 types of environment variables in Linux shell?

Most Common Environment VariablesHOME – The user's home directory location. SHELL – Current shell (bash, zsh, etc.). LOGNAME – Name of the user.

Are environment variables shell specific?

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.


1 Answers

Solution for Ubuntu: set the variables in /etc/environment, and it works for all users and all types of shells.

like image 131
Trung Quan Vo Avatar answered Sep 21 '22 22:09

Trung Quan Vo