0

Normally, in bash, the answer would be ~/.bashrc or /etc/bash.bashrc. But unfortunately Ubuntu is sourcing these files from ~/.profile and /etc/profile respectively. So, where should I put such commands on Ubuntu?

See also Why is /etc/profile not invoked for non-login shells? if you are not familiar with these files.

Seb
  • 61

2 Answers2

2

If you open your man bash, you can find somewhere at the line 150:

When an interactive shell that is not a login shell  is  started,  bash
reads  and  executes  commands  from /etc/bash.bashrc and ~/.bashrc, if
these files exist.  This may be inhibited by using the  --norc  option.
The  --rcfile  file option will force bash to read and execute commands
from file instead of /etc/bash.bashrc and ~/.bashrc.

So, you can use with confidence ~/.bashrc (or /etc/bash.bashrc, but I will not advice you to use this system wide file) for your purpose. And yes, in Ubuntu.

Radu Rădeanu
  • 174,089
  • 51
  • 332
  • 407
0

It appears there is no standard place for such commands on Ubuntu (without modifying behaviour that may be considered standard on Ubuntu). Here is a work-around that I came up with:

Add this to the beginning of /etc/profile:

IS_LOGIN_SHELL=1

Then, in /etc/bash.bashrc or ~/.bashrc test for this variable:

if [ -n "${IS_LOGIN_SHELL-}" ]; then
   # Put your commands here
fi

Hopefully the variable name won't collide with anything else and this doesn't change what other parts of Ubuntu may rely on.

Seb
  • 61