This seems obvious, right? You just do:
and that’s it, correct?
Nope, that gives you the user’s default shell. If the user’s shell is bash, and a script has a shebang (#!) in the first line to specify a different shell (e.g. #!/bin/ksh), the above will say it’s “/bin/bash“. And that’s not what we want here.
Some quick googling was in order, and most suggestions were either:
or:
These work fine in an interactive shell… but inside a script, they’ll reply with the script‘s path, not the shell.
Finally, I found one which worked. It requires /proc, which may not exist in some Unixes, but, AFAIK, it’s mostly universal in Linux:
So, if, for instance, a user’s default shell is bash, the script:
will reply (correctly) with something like (on a Red Hat 8.x system; other distros may have different versions of ksh and/or paths, by default):
/usr/bin/ksh93