Ubuntu – How to the script determine whether it’s being run by bash or dash

bashdash-shellscripts

I'm running a fresh Oneiric install (i.e. not an upgrade) on two different systems and running into the same set of seemingly related problems.

The most frustrating of the bunch is that, when I use the .profile and .bashrc that I have carried with me from Mac OS X, logging in to X via LightDM logs me out immediately. I believe this is caused by the fact that, when running "/bin/sh", it behaves as /bin/dash, but still has the $SHELL variable set to /bin/bash.

Extrapolation

I have a huge .bashrc. You can see it here if you want, but its contents are probably not relevant, aside from the fact that it's full of bashisms, and the fact that it works with no errors inside xterm or on a virtual console.

My .profile looks like this (abbreviated):

case $SHELL in 
*bash*)
    if [ -f $HOME/.bashrc -a -r $HOME/.bashrc ]; then
        . $HOME/.bashrc
    fi
    ;;
esac

If I attempt to log in to X via LightDM, it will log me immediately back out. I get errors in .xsession-errors relating to my .bashrc that look like this (abbreviated):

/home/mrled/.bashrc: 103: [[: not found
[: 103: Linux: unexpected operator
[: 274: -P :: unexpected operator
/home/mrled/.bashrc: 520: complete: not found

As I said, when I run bash from a virtual console, I don't get these errors. Furthermore, if I remove my .profile, I can log in to X just fine. (I can also log in to a virtual console and use startx to initiate an X session that works, but this is of course not a long term solution.)

However, I discovered that if I run /bin/sh -l, I do get the errors. Here's an example session (note: the bash prompt I have simplified to bash>, and the sh prompt is just $):

bash> echo $SHELL
/bin/bash
bash> echo $BASH_VERSION
4.2.10(1)-release
bash> /bin/sh -l
/home/mrled/.bashrc: 103: [[: not found
[: 103: Linux: unexpected operator
[: 274: -P :: unexpected operator
/home/mrled/.bashrc: 520: complete: not found
$ echo $SHELL
/bin/bash
$ echo $BASH_VERSION

$

Q1: Why is this happening?

I understand that /bin/sh now points to dash rather than bash, but if that's true, then why is $SHELL still returning /bin/bash?

Q2: What can I do to work around it?

Is there a way to work around this? I want to keep my profile loading .bashrc so that I get the same environment on both login and non-login shells, but obviously I only want it to load for bash itself, not /bin/sh masquerading as bash.

You may have noticed the difference in the contents of the $BASH_VERSION variables above. I have tried wrapping my .profile in something like this:

if [ -n $BASH_VERSION ]; then
    # the rest of my .profile as above
fi

The -n test should return true only if the length of the string is non-zero, however, even though in the session above, when I'm running under /bin/sh -l it returns an empty string for $BASH_VERSION, when it's included in my .profile like this, it passes the test! It them proceeds to source my .bashrc and give me the same errors as before.

Now I'm really confused.

Best Answer

You can make the fact that $BASH_VERSION is blank in dash work for you:

if [ "$BASH_VERSION" = '' ]; then
    echo "This is dash."
else
    echo "This is bash."
fi