简体   繁体   English

linux中的shell内置命令是什么?

[英]what are shell built-in commands in linux?

I have just started using Linux and I am curious how shell built-in commands such as cd are defined. 我刚刚开始使用Linux,我很好奇如何定义诸如cd类的shell内置命令。

Also, I'd appreciate if someone could explain how they are implemented and executed. 此外,如果有人能解释他们如何实施和执行,我将不胜感激。

If you want to see how bash builtins are defined then you just need to look at Section 4 of The Bash Man Page . 如果你想看看如何定义 bash内置,那么你只需要看看The Bash Man Page的第4节

If, however, you want to know how bash bultins are implemented , you'll need to look at the Bash source code because these commands are compiled into the bash executable. 但是,如果您想知道如何实现 bash bultins,则需要查看Bash源代码,因为这些命令被编译为bash可执行文件。

One fast and easy way to see whether or not a command is a bash builtin is to use the help command. 查看命令是否为bash内置的一种快速简便的方法是使用help命令。 Example, help cd will show you how the bash builtin of 'cd' is defined. 例如, help cd将向您展示如何定义'cd'的bash内置。 Similarly for help echo . 同样为了help echo

The actual set of built-ins varies from shell to shell. 内置插件的实际集合因shell而异。 There are: 有:

  • Special built-in utilities , which must be built-in, because they have some special properties 特殊的内置实用程序 ,必须内置,因为它们具有一些特殊属性
  • Regular built-in utilities , which are almost always built-in, because of the performance or other considerations 由于性能或其他考虑因素, 常规内置实用程序几乎总是内置的
  • Any standard utility can be also built-in if a shell implementer wishes. 如果shell实现者希望,也可以内置任何标准实用程序。

You can find out whether the utility is built in using the type command, which is supported by most shells (although its output is not standardized). 您可以使用type命令查看该实用程序是否已内置,而大多数shell都支持该命令(尽管其输出未标准化)。 An example from dash : dash一个例子:

$ type ls
ls is /bin/ls
$ type cd
cd is a shell builtin
$ type exit
exit is a special shell builtin

Re cd utility, theoretically there's nothing preventing a shell implementer to implement it as external command. Re cd实用程序,理论上没有什么能阻止shell实现者将其实现为外部命令。 cd cannot change the shell's current directory directly, but, for instance, cd could communicate new directory to the shell process via a socket. cd不能直接更改shell的当前目录,但是,例如, cd可以通过套接字将新目录传递给shell进程。 But nobody does so because there's no point. 但没有人这样做,因为没有意义。 Except very old shells (where there was not a notion of built-ins), where cd used some dirty system hack to do its job. 除了非常旧的shell(没有内置插件的概念), cd使用了一些脏系统hack来完成它的工作。

How is cd implemented inside the shell? 如何在shell中实现cd The basic algorithm is described here . 这里描述基本算法。 It can also do some work to support shell's extra features. 它还可以做一些工作来支持shell的额外功能。

Manjari,从ftp://ftp.gnu.org/gnu/bash/bash-2.05b.tar.gz查看bash shell的源代码你会发现shell内置命令的定义不在单独的二进制文件中可执行文件,但它在shell二进制文件本身内(内置的名称shell清楚地暗示了这一点)。

Every Unix shell has at least some builtin commands. 每个Unix shell至少都有一些内置命令。 These builtin commands are part of the shell, and are implemented as part of the shell's source code. 这些内置命令是shell的一部分,并作为shell源代码的一部分实现。 The shell recognizes that the command that it was asked to execute was one of its builtins, and it performs that action on its own, without calling out to a separate executable. shell认识到它被要求执行的命令是它的一个内置命令,并且它自己执行该操作,而不调用单独的可执行文件。 Different shells have different builtins, though there will be a whole lot of overlap in the basic set. 不同的shell具有不同的内置,尽管在基本集中会有很多重叠。

Sometimes, builtins are builtin for performance reasons. 有时,内置程序是出于性能原因而构建的。 In this case, there's often also a version of that command in $PATH (possibly with a different feature set, different set of recognized command line arguments, etc), but the shell decided to implement the command as a builtin as well so that it could save the work of spawning off a short-lived process to do some work that it could do itself. 在这种情况下, $PATH通常还有该命令的一个版本(可能具有不同的功能集,不同的识别命令行参数集等),但shell决定将该命令作为内置实现,以便它可以节省一个短暂的过程产生的工作,以完成它自己可以做的一些工作。 That's the case for bash and printf, for example: 就像bash和printf一样,例如:

$ type printf
printf is a shell builtin
$ which printf
/usr/bin/printf
$ printf
printf: usage: printf [-v var] format [arguments]
$ /usr/bin/printf
/usr/bin/printf: missing operand
Try `/usr/bin/printf --help' for more information.

Note that in the above example, printf is both a shell builtin (implemented as part of bash itself), as well as an external command (located at /usr/bin/printf). 请注意,在上面的示例中,printf既是shell内置的(作为bash本身的一部分实现),也是外部命令(位于/ usr / bin / printf)。 Note that they behave differently as well - when called with no arguments, the builtin version and the command version print different error messages. 请注意,它们的行为也不同 - 当不带参数调用时,内置版本和命令版本会打印不同的错误消息。 Note also the -v var option (store the results of this printf into a shell variable named var ) can only be done as part of the shell - subprocesses like /usr/bin/printf have no access to the variables of the shell that executed them. 另请注意-v var选项(将此printf的结果存储到名为var的shell变量中)只能作为shell的一部分完成 - 像/ usr / bin / printf这样的子进程无法访问执行的shell变量他们。

And that brings us to the 2nd part of the story: some commands are builtin because they need to be. 这将我们带到故事的第二部分:一些命令是因为它们需要而构建的。 Some commands, like chmod , are thin wrappers around system calls. 一些命令,如chmod ,是围绕系统调用的薄包装器。 When you run /bin/chmod 777 foo , the shell forks, execs /bin/chmod (passing "777" and "foo") as arguments, and the new chmod process runs the C code chmod("foo", 777); 当你运行/bin/chmod 777 foo ,shell forks,execs / bin / chmod(传递“777”和“foo”)作为参数,而新的chmod进程运行C代码chmod("foo", 777); and then returns control to the shell. 然后将控制权返回给shell。 This wouldn't work for the cd command, though. 但这不适用于cd命令。 Even though cd looks like the same case as chmod , it has to behave differently: if the shell spawned another process to execute the chdir system call, it would change the directory only for that newly spawned process, not the shell. 尽管cd看起来与chmod情况相同,但它必须表现得不同:如果shell产生另一个进程来执行chdir系统调用,它将仅为新生成的进程而不是shell更改目录。 Then, when the process returned, the shell would be left sitting in the same directory as it had been in all along - therefore cd needs to be implemented as a shell builtin. 然后,当进程返回时,shell将保持原样位于同一目录中 - 因此cd需要实现为内置shell。

A Shell builtin -- http://linux.about.com/library/cmd/blcmdl1_builtin.htm for eg. 内置的Shell - 例如http://linux.about.com/library/cmd/blcmdl1_builtin.htm - -

which cd 
/usr/bin/which: no cd in (/usr/bin:/usr/local/bin......

Not a shell builtin but a binary. 不是内置的shell而是二进制的。

which ls
/bin/ls

http://ss64.com/bash/ this will help you. http://ss64.com/bash/这会对你有所帮助。

and here is shell scripting guide 这是shell脚本指南

http://www.freeos.com/guides/lsst/ http://www.freeos.com/guides/lsst/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM