#### running under "some" shell

# This script will detect which shell is being used and print the name
# thereof.  It can handle the following, as they exist on my Ubuntu
# Xenial box:
#
#    bash
#    bsd-csh
#    dash
#    ksh93
#    zsh5
#
# Note: on my box I observe:
#     ash is a symlink to dash
#     csh is a symlink to /bin/bsd-csh
#     ksh is a symlink to /bin/ksh93
#      sh is a symlink to dash

# This version has a modest level of redundancy: It makes two
# different checks, so a small change in one of the shells would be
# detected.  Much more complex checks are possible.  There is no
# guarantee that this version is robust across all past and/or future
# versions of the shells.

#########
# Safe detection of csh requires some cleverness:
/usr/bin/test _`echo asdf 2>/dev/null` != _asdf \
        && echo bsd-csh && exit

# Now that csh has been excluded, it is safe to continue
########

res1=$(export PATH=/dev/null/$$
  type -p 2>/dev/null)
st1="$?"

res2=$(export PATH=/dev/null/$$
  type declare 2>/dev/null)
st2="$?"        # not

# this version works without sed, and indeed without a $PATH
penult='nil'  ult=''
for word in $(echo $res2) ; do
  penult="$ult"
  ult="$word"
done

tag="${st1}.${penult}_${ult}"
case "${tag}" in
 0.shell_builtin) echo bash  ; exit ;;
   127.not_found) echo dash  ; exit ;;
          2.nil_) echo ksh93 ; exit ;;
 1.reserved_word) echo zsh5  ; exit ;;
esac

# We hope never to get here.
echo unknown : "$tag"
exit 2
