2

From this question I understand that depending on the operating system sh and bash behave differently.

Doing env and alias I can tell that:

Every time I open a OSX Terminal or run bash the variable $SHELL=/bin/bash runs source ~/.profile which calls source ~/.bashrc. If I run $sh the commands source ~/.profile nor source ~/.bashrc are not called, hence none of the aliases is defined.

Now on a Linux or UNIX server using bash or sh:

How can I tell what scripts run at login?

1

2 Answers 2

2

First identify your shell, as noted in another answer this is simply done via the ps command ...

$ ps
  PID TTY          TIME CMD
 3360 pts/2    00:00:00 bash
 3702 pts/2    00:00:00 ps

To be sure which files are being read you will need to investigate the system which you are logging into. 'bash' shell has many 'if this' then 'do that' otherwise 'do something else' options. To see a full description of what files are sourced see here, but typically the list is ...

  • /etc/profile
  • $HOME/.bash_profile
  • $HOME/.bashrc (if that is configured in the .bash_profile)
  • $HOME/.bash_logout (on logout, not login)

For the bourne shell ('sh') it generally goes as follows when logging in

  • /etc/profile
  • $HOME/.profile

What I like to do on systems I am unfamiliar with is a line in any start up files I am expecting to be read that tests if the standard output is connected to a terminal, and if it is then display the file name. In this example I am using it in the .bash_profile file.

[ -t 1 ] && echo "reading .bash_profile"

This works for either bash or sh (and a few other derivatives as well). This helps because I know what is being read, when, and if I am getting errors I know which file they came from (errors before the first echoed message are likely from the login process itself, or from the system wide /etc/profile.

Also don't forget you can always create your own bespoke start up files and include them yourself by including them in a file which is already being sourced.

1
  • Thanks for the detailed answer and the recommendation. I see that OSX is very different compared to Linux. Lesson of the day: cat \etc\profile
    – ilciavo
    Commented Jul 21, 2015 at 16:55
1
  1. /etc/passwd last field of /etc/passwd will tell you your loggin shell.
  2. ps running ps will tell you also (and level of shell)

      PID TTY          TIME CMD
     5802 pts/0    00:00:00 bash
     6292 pts/0    00:00:00 ps
    
1
  • thanks, where are source ~/.profile, source ~/.bashrc being call with bash or sh?
    – ilciavo
    Commented Jul 21, 2015 at 14:23

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .