In my .profile
, I use the following code to ensure that Bash-related aliases and functions are only sourced if the login shell actually is Bash:
# If the current (login) shell is Bash, then
if [ "${BASH_VERSION:-}" ]; then
# source ~/.bashrc if it exists.
if [ -f "$HOME/.bashrc" ]; then
. "$HOME/.bashrc"
fi
fi
I’m currently in the process of putting my shell configuration files, scripts and functions under version control. I’ve also recently started the process of removing casual Bashisms from shell scripts that don’t benefit from Bash-specific features, e.g., replacing function funcname()
with funcname()
.
For my shell files repository, I’ve configured a pre-commit hook that runs the checkbashisms
utility from Debian’s devscripts package on each sh
file in the repository to ensure that I don’t inadvertently introduce Bash-specific syntax. However, this raises an error for my .profile
:
possible bashism in .profile line 51 ($BASH_SOMETHING):
if [ "${BASH_VERSION:-}" ]; then
I was wondering if there was a way to check which shell is running that wouldn’t trigger a warning in checkbashisms
.
I checked the list of shell-related variables listed by POSIX in the hope that one of them could used to show the current shell. I’ve also looked at the variables set in an interactive Dash shell but, again, failed to find a suitable candidate.
At the moment, I’ve excluded .profile
from being processed by checkbashisms
; it’s a small file so it’s not hard to check it manually. However, having researched the issue, I’d still like to know if there is a POSIX compliant method to determine which shell is running (or at least a way that doesn’t cause checkbashisms
to fail).
Further background/clarification
One of the reasons I’m putting my shell configuration files under version control is to configure my environment on all the systems I currently log in to on a regular basis: Cygwin, Ubuntu and CentOS (both 5 and 7, using Active Directory for user authentication). I most often log on via X Windows / desktop environments and SSH for remote hosts. However, I’d like this to be future proof and have the least reliance on system dependencies and other tools as possible.
I’ve been using checkbashisms
as a simple, automated sanity check for the syntax of my shell-related files. It’s not a perfect tool, e.g., I’ve already applied a patch to it so that it doesn’t complain about the use of command -v
in my scripts. While researching, I’ve learned that the program’s actual purpose is to ensure compliance with Debian policy which, as I understand it, is based on POSIX 2004 rather than 2008 (or its 2013 revision).
.bash_profile
that sources both.profile
and (conditionally).bashrc
.