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.
! -n
or anything else I've asked. – AbraCadaver Jan 22 '20 at 20:26