我怎么知道我的壳是什么类型的

时间:2022-12-30 07:21:16

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

我怎么知道我的壳是什么类型的?无论是传统的sh, bash, ksh, csh, zsh等。

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.

注意,检查$SHELL或$0将不起作用,因为$SHELL不是由所有SHELL设置的,所以如果您从一个SHELL开始,然后再开始另一个SHELL,您可能仍然拥有旧的$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.

$0只告诉您shell二进制文件的位置,但不告诉您/bin/sh是真正的Bourne shell还是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.

我想答案应该是“尝试一些特性,看看会出现什么问题”,所以如果有人能给我指出这样做的脚本,那就太棒了。

7 个解决方案

#1


21  

This is what I use in my .profile:

这是我在我的。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.)

它没有对ksh88、ksh95、pdksh或mksh等进行细致的区分,但在十多年的时间里,它已经被证明对我在家里的所有系统(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.

我不认为需要在.profile中检查csh,因为csh在启动时从其他文件中获取。您编写的任何脚本都不需要检查csh和Bourne-heritage,因为您在shebang行中显式地命名了解释器。

#2


14  

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

尝试使用当前shell PID定位shell路径:

ps -p $$

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

它至少应该与sh、bash和ksh一起工作。

#3


4  

If the reason you're asking is to try to write portable shell code, then spotting the shell type, and switching based on it, is an unreliable strategy. There's just too much variation possible.

如果您要求的原因是尝试编写可移植的shell代码,然后找出shell类型并基于该类型进行切换,那么这是一种不可靠的策略。有太多的变化可能。

Depending on what you're doing here, you might want to look at the relevant part of the autoconf documentation. That includes an interesting (and in some respects quite dismal) zoology of different shell aberrations.

根据您在这里所做的操作,您可能需要查看autoconf文档的相关部分。这包括一个有趣的(在某些方面相当令人沮丧的)不同的壳像差的动物学。

For the goal of portable code, this section should be very helpful. If you do need to spot shell variants, then there might be some code buried in autoconf (or at least in one of the ./configure scripts it generates) which will help with the sniffing.

对于可移植代码的目标,本节应该非常有用。如果您确实需要发现shell变体,那么autoconf中可能包含一些代码(或者至少在它生成的./configure脚本中),这将有助于嗅探。

#4


3  

You can use something like this:

你可以这样使用:

shell=`cat /proc/$$/cmdline`;

#5


0  

Oh, I had this problem. :D

哦,我有这个问题。:D

There is a quick hack, use ps -p $$ command to list the process with PID of the current running process -- which is your SHELL. This returns a string table structure, if you want, you can AWK, or SED the shell out...

有一个简单的方法,使用ps -p $$命令列出当前正在运行的进程(即SHELL)的PID。这将返回一个字符串表结构,如果您愿意,您可以AWK或SED shell out…

#6


0  

It's old thread but...

这是老帖子,但是……

In GNU environment You can sh --help and get something like

在GNU环境中,您可以sh -help并获得类似的东西

BusyBox v1.23.2 (2015-04-24 15:46:01 GMT) multi-call binary.

Usage: sh [-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]

Unix shell interpreter

So, the first line is shell type =)

第一行是shell类型=)

#7


-1  

The system shell is the thing you see when you open up a fresh terminal window which is not set to something other than bash (assuming this is your default SHELL).

当您打开一个新的终端窗口时,系统shell是您看到的东西,它不是设置为bash之外的东西(假设这是您的默认shell)。

echo $SHELL

Generally, you can find out all the constants defined by running

通常,您可以找到运行所定义的所有常量。

set

If the output is a lot of stuff then run

如果输出很多,那么运行

set | less

集|少

so you can scroll it from the top of the command line or

所以你可以从命令行或者

set > set.txt

设置> set.txt

To save the output to a file.

将输出保存到文件中。

Invoking a different interactive shell to bash in your terminal does not mean that your system shell gets changed to something else i.e. your system shell is set to bash although you invoke a csh shell from a bash shell just that one session.

调用一个不同的交互shell来在终端中进行bash并不意味着您的系统shell被更改为其他东西,例如,您的系统shell被设置为bash,尽管您只是从bash shell中调用一个csh shell。

The above means that typing /bin/csh or /bin/python in bash or whatever does not set the system shell to the shell you invoked, at all.

上面的意思是,在bash中输入/bin/csh或/bin/python,或者其他什么,根本不会将系统shell设置为您所调用的shell。

If you really want to see the SHELL constant change then you need to set it to something else. If successful you should see the new shell whenever you open a fresh terminal...

如果你真的想看到壳层常数的变化,那么你需要把它设置成别的东西。如果成功的话,当你打开一个新的终端时,你应该会看到新的外壳。

#1


21  

This is what I use in my .profile:

这是我在我的。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.)

它没有对ksh88、ksh95、pdksh或mksh等进行细致的区分,但在十多年的时间里,它已经被证明对我在家里的所有系统(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.

我不认为需要在.profile中检查csh,因为csh在启动时从其他文件中获取。您编写的任何脚本都不需要检查csh和Bourne-heritage,因为您在shebang行中显式地命名了解释器。

#2


14  

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

尝试使用当前shell PID定位shell路径:

ps -p $$

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

它至少应该与sh、bash和ksh一起工作。

#3


4  

If the reason you're asking is to try to write portable shell code, then spotting the shell type, and switching based on it, is an unreliable strategy. There's just too much variation possible.

如果您要求的原因是尝试编写可移植的shell代码,然后找出shell类型并基于该类型进行切换,那么这是一种不可靠的策略。有太多的变化可能。

Depending on what you're doing here, you might want to look at the relevant part of the autoconf documentation. That includes an interesting (and in some respects quite dismal) zoology of different shell aberrations.

根据您在这里所做的操作,您可能需要查看autoconf文档的相关部分。这包括一个有趣的(在某些方面相当令人沮丧的)不同的壳像差的动物学。

For the goal of portable code, this section should be very helpful. If you do need to spot shell variants, then there might be some code buried in autoconf (or at least in one of the ./configure scripts it generates) which will help with the sniffing.

对于可移植代码的目标,本节应该非常有用。如果您确实需要发现shell变体,那么autoconf中可能包含一些代码(或者至少在它生成的./configure脚本中),这将有助于嗅探。

#4


3  

You can use something like this:

你可以这样使用:

shell=`cat /proc/$$/cmdline`;

#5


0  

Oh, I had this problem. :D

哦,我有这个问题。:D

There is a quick hack, use ps -p $$ command to list the process with PID of the current running process -- which is your SHELL. This returns a string table structure, if you want, you can AWK, or SED the shell out...

有一个简单的方法,使用ps -p $$命令列出当前正在运行的进程(即SHELL)的PID。这将返回一个字符串表结构,如果您愿意,您可以AWK或SED shell out…

#6


0  

It's old thread but...

这是老帖子,但是……

In GNU environment You can sh --help and get something like

在GNU环境中,您可以sh -help并获得类似的东西

BusyBox v1.23.2 (2015-04-24 15:46:01 GMT) multi-call binary.

Usage: sh [-/+OPTIONS] [-/+o OPT]... [-c 'SCRIPT' [ARG0 [ARGS]] / FILE [ARGS]]

Unix shell interpreter

So, the first line is shell type =)

第一行是shell类型=)

#7


-1  

The system shell is the thing you see when you open up a fresh terminal window which is not set to something other than bash (assuming this is your default SHELL).

当您打开一个新的终端窗口时,系统shell是您看到的东西,它不是设置为bash之外的东西(假设这是您的默认shell)。

echo $SHELL

Generally, you can find out all the constants defined by running

通常,您可以找到运行所定义的所有常量。

set

If the output is a lot of stuff then run

如果输出很多,那么运行

set | less

集|少

so you can scroll it from the top of the command line or

所以你可以从命令行或者

set > set.txt

设置> set.txt

To save the output to a file.

将输出保存到文件中。

Invoking a different interactive shell to bash in your terminal does not mean that your system shell gets changed to something else i.e. your system shell is set to bash although you invoke a csh shell from a bash shell just that one session.

调用一个不同的交互shell来在终端中进行bash并不意味着您的系统shell被更改为其他东西,例如,您的系统shell被设置为bash,尽管您只是从bash shell中调用一个csh shell。

The above means that typing /bin/csh or /bin/python in bash or whatever does not set the system shell to the shell you invoked, at all.

上面的意思是,在bash中输入/bin/csh或/bin/python,或者其他什么,根本不会将系统shell设置为您所调用的shell。

If you really want to see the SHELL constant change then you need to set it to something else. If successful you should see the new shell whenever you open a fresh terminal...

如果你真的想看到壳层常数的变化,那么你需要把它设置成别的东西。如果成功的话,当你打开一个新的终端时,你应该会看到新的外壳。