1

I have access to several RHEL systems that use Active Directory for user accounts and a NFS mount for home directories.

Our login shell is stored in Active Directory and is always /bin/ksh. I want to always have /bin/bash, so I figured I could accomplish this in ~/.profile. After reading several posts here and on SO I have come up with this:

if [ ! -n "$BASH" ] ;then
    SHELL=`type -P bash`
    exec bash -l;
fi
  • Is this a standard/good way of doing this?
  • I read ! -n "$BASH" as NOT ("$BASH" is NOT null)
    • So true if it IS null and false if NOT null.
    • If so then why not just -z "$BASH" or even ! "$BASH"?

1 Answers1

3

The code that you show could very well be used to start a bash login shell from your ~/.profile file. I personally would use command to detect the presence of bash like so:

if [ -z "$BASH" ]; then
    shell=$( command -v bash )
    if [ -n "$shell" ]; then
        exec env SHELL="$shell" bash --login
    fi
    unset shell
    echo 'Bash not present, continuing...' >&2
fi

This gets the path to bash into $shell which is then used in a test of whether bash is available and to set the SHELL environment variable for bash.

Whether to use [ ! -n "$BASH" ] or [ -z "$BASH" ] is of minor importance (so do whatever feels right and is easiest to read), but I'd probably not use ! [ "$BASH" ] or [ ! "$BASH" ]. In the general case you may end up testing strings that start with dashes, which could confuse the test, so it's better stick to -n or -z when testing whether strings are empty or not.

You may also want to keep ~/.profile largely untouched (apart from the above) if the admins of the system one day decides to uninstall bash, and instead write a separate ~/.bash_profile file. The bash shell would use ~/.bash_profile, if it is available, instead of ~/.profile, and a failure in launching bash would result in your default login shell successfully using ~/.profile without stumbling over possible bashisms.

Kusalananda
  • 333,661
  • Thanks! Won't "$shell" bash -l eval to /bin/bash bash -l? What is that? – AbraCadaver Jan 22 '20 at 20:38
  • @AbraCadaver No, exec env SHELL="$shell" bash -l would expand to exec env SHELL="/bin/bash" bash -l which replaces the current shell with bash and sets SHELL to /bin/bash. The env utility is used to execute another command and setting environment variables for that command. – Kusalananda Jan 22 '20 at 20:41