Skip to content

What the (S)hell

TomfromBerlin edited this page Mar 30, 2023 · 8 revisions
I can't stress this enough: If you use these files in shells other than bash, I strongly recommend checking the contents for compatibility.

The idea that came up with the what_shell file was to make it accessible to other shells as well and if possible, to prevent that any user-side changes to the files are necessary for it to work - apart from renaming the .bashrc file. For now, this query (and I am talking here only about this query, read below) should work with the following shells:

  1. Bourne Again Shell (bash) ✔️
  2. Bourne Shell (sh)
  3. Almquist-Shell (ash)
  4. Debian-Almquist-Shell (dash)
    • When using dash as an interactive shell it is recommended, to check that all scripts that don't have the #!/bin/bash directive in their shebang are fully POSIX compliant or you will get in trouble. This may apply also to Almquist Shell (ash).
  5. Korn Shell (ksh)
  6. MirBSD Korn Shell (mksh)
  7. Puplic Domain Korn Shell (pdksh)
  8. Policy-compliant Ordinary SHell (posh) - based on pdksh (Do not confuse it with Oh My Posh)
  9. Yet Another Shell (yash)
  10. Z Shell (zsh) ✔️

The Friendly Interactive Shell (fish) will not work as it is not compatible with any other shell.

However, there is another tiny problem: Different shells have different filenames and especially different features and functions. Also, the commands may vary depending on which shell is used and whether that is also the default shell or possibly called from another shell, which can lead to different results. This may require different queries. Therefore, the file ~/.shellcfg/what_shell is currently only safe to use in the Bourne Again Shell (bash), Bourne Shell (sh), Z Shell, and should not cause any problems here, but may work within Korn-Shell nad derivates (ksh, pdksh mksh) and YASH as well. But as I said, all the shells mentioned are initially recognized. However, whether the rest will also work is not certain at the moment.

In principle, a - more or less simple - query is sufficient to find out the name of the shell. As usual in Linux, there is more than one way to achieve a goal. To check which shell is being used, the following commands can be used:

  • echo $SHELL - the easiest way and the most commonly used, and displays the default shell for the system (or user), which is usually (but not necessarily) the shell actually being used at any given time
  • ps -cp "$$" -o command="" - shows the actual shell currently in use, but that wouldn't always work everywhere, e.g. in Tenex-C-Shell (tcsh) it will not

But the standard output for echo $SHELL is ugly: /bin/bash (e.g. for bash)

ps -cp "$$" -o command="" isn't much better either: bash

Yes it's functional and yes it's ugly. Because of this, I wrote a few lines to change the displayed information. That's why the terminal says i.e. "Bourne Again Shell (bash) version-no" and not "bash version-no".

Query Version

The version query should work in bourne-compatible shells (sh, bash, ksh, zsh, posh and yash). This does not apply to dash as dash has no information about its version for historical reasons. Inside the ksh93 Korn shell, $KSH_VERSION wasn't implemented before 93t (so not in ksh88 either), but are you really using that outdated version?

If you want to really deep dive into the abyss you can find here information about a much more sophisticated attempt to determine the very version of all bourne shell and all bourne-compatible shells. Here is the link to the script itself.