Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I tell what type my shell is

Tags:

How can I tell what type my shell is? ie, whether it's traditional sh, bash, ksh, csh, zsh etc.

Note that checking $SHELL or $0 won't work because $SHELL isn't set by all shells, so if you start in one shell and then start a different one you may still have the old $SHELL.

$0 only tells you where the shell binary is, but doesn't tell you whether /bin/sh is a real Bourne shell or bash.

I presume that the answer will be "try some features and see what breaks", so if anyone can point me at a script that does that, that'd be great.

like image 422
DrHyde Avatar asked Mar 02 '11 11:03

DrHyde


People also ask

How do I know my shell type?

ps -p $$ – Display your current shell name reliably. echoechoIn computing, echo is a command that outputs the strings that are passed to it as arguments. It is a command available in various operating system shells and typically used in shell scripts and batch files to output status text to the screen or a computer file, or as a source part of a pipeline.https://en.wikipedia.org › wiki › Echo_(command)echo (command) - Wikipedia "$SHELL" – Print the shell for the current user but not necessarily the shell that is running at the movement. echo $0 – Another reliable and simple method to get the current shell interpreter name on Linux or Unix-like systems.

Is my shell bash or zsh?

Now, coming to the article's main subject, how will you know that you have bash or zsh? The answer is quite simple. Use the “–version” command to confirm the existence of both shells on your Linux system.

How do I know which shell my Mac has?

There's an easy way to tell — here's how. Open the Terminal application on your Mac. At the prompt, type echo $0 , as shown below.


2 Answers

This is what I use in my .profile:

# .profile is sourced at login by sh and ksh. The zsh sources .zshrc and
# bash sources .bashrc. To get the same behaviour from zsh and bash as well
# I suggest "cd; ln -s .profile .zshrc; ln -s .profile .bashrc".
# Determine what (Bourne compatible) shell we are running under. Put the result
# in $PROFILE_SHELL (not $SHELL) so further code can depend on the shell type.

if test -n "$ZSH_VERSION"; then
  PROFILE_SHELL=zsh
elif test -n "$BASH_VERSION"; then
  PROFILE_SHELL=bash
elif test -n "$KSH_VERSION"; then
  PROFILE_SHELL=ksh
elif test -n "$FCEDIT"; then
  PROFILE_SHELL=ksh
elif test -n "$PS3"; then
  PROFILE_SHELL=unknown
else
  PROFILE_SHELL=sh
fi

It does not make fine distinctions between ksh88, ksh95, pdksh or mksh etc., but in more than ten years it has proven to work for me as designed on all the systems I were at home on (BSD, SunOS, Solaris, Linux, Unicos, HP-UX, AIX, IRIX, MicroStation, Cygwin.)

I don't see the need to check for csh in .profile, as csh sources other files at startup. Any script you write does not need to check for csh vs Bourne-heritage because you explicitly name the interpreter in the shebang line.

like image 113
Jens Avatar answered Sep 27 '22 20:09

Jens


Try to locate the shell path using the current shell PID:

ps -p $$

It should work at least with sh, bash and ksh.

like image 21
ziu Avatar answered Sep 27 '22 21:09

ziu