Shahzeb Ihsan

"The reasonable man adapts himself to the world; the unreasonable man persists in trying to adapt the world to himself. Therefore, all progress depends on the unreasonable man." -- George Bernard Shaw


Blog | Archive | About Me | Projects

Setting Linux Environment Variables, Bash Init Scripts and Loading/Execution Order

14 Aug 2015 | virtualbox, virtualmachine, archlinux, linux, development, bash, script, etc, environment, pam

This post serves as a reference for various Linux startup, login and init scripts, environment variables and the order in which they are loaded or executed.

I use Arch Linux with Bash, but the general concepts should be applicable to any distro or shell. I also use Bash as my default login shell. You can confirm which login shell you’re running (sh or bash) by using echo $SHELL. The default user shell can be changed using the chsh command.

/etc/environment

~/.bashrc

$BASH_ENV

Note: SSH is an interactive login shell BUT it starts non-interactively. This is how I confirmed this:

$ echo $-
himBH

~/.bash_profile, ~/.bash_login, ~/.profile

For login shells, Bash searches for & executes whichever script it finds in the following order of priority:

/etc/profile

/etc/bash.bashrc

Execution/Loading Order

Interactive/Non-login (e.g. local terminal)

:

Interactive/Login (e.g. virtual terminals opened using Ctrl-Alt-Fn or `tmux`)

:

For SSH

:

References

  1. https://help.ubuntu.com/community/EnvironmentVariables#System-wide_environment_variables
  2. https://wiki.archlinux.org/index.php/Bash
  3. https://bugs.launchpad.net/ubuntu/+source/manpages/+bug/18574/comments/13
  4. http://askubuntu.com/questions/98433/run-a-script-on-login-using-bash-login
  5. http://linux.die.net/man/8/pam_env
  6. http://serverfault.com/questions/593472/where-is-bash-env-usually-set
  7. http://stackoverflow.com/questions/3327013/how-to-determine-the-current-shell-im-working-on
  8. http://www.chainsawonatireswing.com/2012/02/02/find-out-what-your-unix-shells-flags-are-then-change-them//?from=@#fn:curly-brace-expansion
  9. http://capistranorb.com/documentation/faq/why-does-something-work-in-my-ssh-session-but-not-in-capistrano/
comments powered by Disqus

Older · View Archive (8)

Some Arch Linux VM Issues & Their Solutions

Some recurring issues I’ve had with VirtualBox and Arch Linux and their solutions. This bash script implements all these solutions.

Newer

Using SSHFS

A while back I had issues with git clone on Windows host machine’s folders mounted using vboxsf. Cloning, pushing or committing would result in Git complaining that it couldn’t lock the config file because permission was denied. I uselessly Googled for a solution for about a week until I discovered SSHFS.