0

I have a strange problem with bash:

I switch my default shell to /bin/sh from zsh and it doesn't execute ~/.bash_profile or ~/.bashrc when I open a new terminal, it only executes ~/.profile.

What could be the problem?

I could probably source ~/.bashrc from ~/.profile but that does't seem like a good idea, e.g. it would break if I changed to another shell again.

3 Answers 3

2

I suggest that you read the bash manual under the heading INVOCATION which states:

   If bash is invoked with the name sh, it tries to mimic the startup behavior of historical versions of sh as  closely
   as  possible, while conforming to the POSIX standard as well.  When invoked as an interactive login shell, or a non-
   interactive shell with the --login option, it first attempts to read and  execute  commands  from  /etc/profile  and
   ~/.profile,  in that order.  The --noprofile option may be used to inhibit this behavior.  When invoked as an inter-
   active shell with the name sh, bash looks for the variable ENV, expands its value if it is  defined,  and  uses  the
   expanded  value as the name of a file to read and execute.  Since a shell invoked as sh does not attempt to read and
   execute commands from any other startup files, the --rcfile option has no effect.  A non-interactive  shell  invoked
   with the name sh does not attempt to read any other startup files.  When invoked as sh, bash enters posix mode after
   the startup files are read.

A new terminal session in OS X/macOS always starts as a login shell.

0

To get /bin/sh to execute ~/.bashrc the same way /bin/bash does, you would have to do the following.

  1. Add the following lines to ~/.profile.

    LOGINSHELL=YES
    export ENV=~/.shrc
    
  2. Create the file ~/.shrc containing the following line.

    if [ "$LOGINSHELL" != "YES" ]; then  source ~/.bashrc; fi
    

To help in debugging, I usually add the following line to beginning of the ~/.profile file.

export DEBUGSHELL=YES

This line can be commented out when done debugging, as shown below.

#export DEBUGSHELL=YES

Next, add conditional echo statements to the beginning and ending of the files such as ~/.profile, ~/.bashrc, ~/.shrc and others. For example, ~/.shrc would contain the following.

if [ "$DEBUGSHELL" = "YES" ]; then echo entered .shrc; fi
if [ "$LOGINSHELL" != "YES" ]; then  source ~/.bashrc; fi
if [ "$DEBUGSHELL" = "YES" ]; then echo exited .shrc; fi

Another idea, that may be useful, is to create a file ~/.bashcm to contain the commands that need to be executed for both login and interactive shells. For example, this is usually where I place my alias commands. You will also need to include the following line in both your ~/.profile and ~/.bashrc files.

source ~/.bashcm
-1

/bin/sh is not bash. It is Bourne Shell. Bash (Bourne Again Shell)'s location is /bin/bash. Change your default shell to bash to use ~/.bash_profile instead of ~/.profile

1
  • /bin/sh on OSX is bash but it tries to mimic the startup behavior of historical versions of sh as closely as possible, while conforming to the POSIX standard as well. (from other answer)
    – mmmmmm
    Oct 26, 2016 at 12:13

You must log in to answer this question.

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