shell 中内置的工具
我想知道 bash shell 中内置的工具。例如,type pwd
告诉我 pwd
内置于 shell 中。
whereis pwd
/bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz
aptitude search pwd
(似乎)没有给我使用的密码提供任何信息。 (我使用的是 Debian 系统。)
-- 有什么方法可以找出内置的东西吗?除了使用 type
进行暴力破解之外,就是这样。
-- bin
文件夹(上图)中的 pwd
是否与内置的 pwd
相同?它是否在启动时加载到 shell 中?或者它是由 shell 从该文件夹执行的?如果是的话,它是以什么方式内置的?
——为什么首先要内置这些东西?它们是否经过特别调整以适合外壳,或者只是为了可以在内部调用它们,这样它们就不需要新的进程?我设法用 pwd &
和 ps
捕获了 pwd
。这是一种规避还是它们是单独的过程?
-- 请随意告诉我有关该主题的任何其他信息:)
I was wondering about tools that are built into the bash
shell. For example, type pwd
tells me that pwd
is built into the shell.
whereis pwd
/bin/pwd /usr/include/pwd.h /usr/share/man/man1/pwd.1.gz
aptitude search pwd
does not (seem to) give anything on the pwd I use. (I'm on a Debian system.)
-- Is there any way to find out what stuff are built in? Besides brute force with type
, that is.
-- Is the pwd
in the bin
folder (above) the same pwd
that is built in? Is it loaded into the shell at initiation? Or is it executed from that folder by the shell? If so, in what way is it built-in?
-- Why are stuff built in in the first place? Are they especially tweaked to fit the shell, or is it just so that they can be invoked internally so they don't require a new process? I managed to catch a pwd
with pwd &
and ps
. Is this a circumvention or are they separate processes?
-- Feel free to tell me anything else on the topic :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
help
将为您提供完整的列表。您可以使用内置命令作为参数运行help
来获取更详细的信息。info "(bash) Shell 内置命令"
将显示所有内置命令的 Bash 手册。不,它们是完全不同的:
手册中写道:“内置命令对于实现使用单独的实用程序不可能或不方便获得的功能是必要的。”很难让像 cd 这样的命令在外部工作,因为它会影响 shell 的状态。当然,复制
pwd
或true
的行为很容易,但是 POSIX 标准 要求它们是内置的。运行 builtin
&
将导致 Bash 在后台运行子 shell。您可以通过执行read &
轻松看到这一点,因为read
会等待直到有输入。help
will get you a complete list. You can runhelp
with a builtin command as argument to get more detailed information.info "(bash) Shell Builtin Commands"
will display the Bash manual for all the builtins.No, they are completely different:
From the manual: "Builtin commands are necessary to implement functionality impossible or inconvenient to obtain with separate utilities." It would be hard to make a command like
cd
work externally because it affects the state of the shell. Of course, it is easy to duplicate the behavior ofpwd
ortrue
, but the POSIX standard requires that they are built-ins.Running builtin
&
will cause Bash to run a subshell in the background. You can see this easily by doingread &
, sinceread
waits until it has input.为了回答你的第一个问题,我发现如果我(在我的 bash shell 中)输入“builtin”,然后输入 tab-tab,它会显示内置命令的列表,因为它具有制表符补全功能。我知道这只是你要问的一小部分,但这是一个开始,因为我不知道所有“为什么”的东西。 :P
To answer your first question, I found that if I type (in my bash shell) "builtin" and then tab-tab, it shows me a list of the builtins, since it has tab-completion. I know it's only a small part of what you're asking, but it's a start, since I don't know all the "why" stuff. :P
Shell 内置程序显然比单独的二进制文件更有效。单独的二进制文件是完全独立的,基本上可以与其他没有内置这些东西的 shell 一起使用。您可以通过将命令放在引号中来强制 bash 使用二进制文件,IIRC。如果您
man bash
,您会发现很多有关内置命令及其工作原理的信息(它并不总是与外部二进制文件相同)。Shell builtins are obviously more efficient than separate binaries. The separate binaries are totally independent and are basically for use with other shells that do not have this stuff builtin. You can force bash to use binary by putting the command in quotes, IIRC. If you
man bash
you will find quite some information on builtin commands and how exactly they work (it's not always the same like external binaries).bash
手册页 (man bash
) 枚举了内置函数。which
命令将列出找到非内置命令的位置,同时考虑到${PATH}
等。/bin/
中的文件是替换文件,以防您使用的 shell 没有将它们作为内置文件。它们是为了提高效率而内置的,以避免
fork
/exec
,但bash
特别是通常足够聪明,仍然可以fork
必要时(例如,对于|
或&
操作)如果您确实需要运行
bin< 中的版本/code>,您可以通过完整路径名调用它们(例如
/bin/pwd
)。 (这也绕过了alias
等。) — 这对于bash
来说很少有用,但如果您使用的是非常简单的嵌入式 shell,例如busybox< /code>,这可能很有帮助,因为它们的内置函数通常是功能的子集。
The
bash
manual page (man bash
) enumerates the built-ins.which
command will list where non-built-ins are found, taking into account${PATH}
and the like.The files in
/bin/
are replacements, in case you use a shell that doesn't have them as builtins.They're built in for efficiency, to avoid a
fork
/exec
, butbash
in particular is usually smart enough to stillfork
when necessary (e.g. for|
or&
operations)If you do need to run the versions in
bin
, you can invoke them by full pathname (/bin/pwd
for example). (This also circumventsalias
and the like.) — This is rarely useful withbash
, but if you're using a very simple embedded shell, e.g.busybox
, this can be helpful, as their builtins are often subsets of the functionality.有关 shell 内置命令的完整列表,请使用
man bash
。内置命令是 shell 在实际 shell 可执行文件中编译的命令。这样,如果由于某种原因您没有echo
命令,您仍然可以从 shell 运行它。这对于精简系统(例如嵌入式设备)或意外擦除硬盘驱动器部分的情况非常方便。通常,内置程序比外部可执行文件受到更多限制,但它也不需要启动另一个进程,而您可能没有足够的资源来执行此操作。同样,大多数人不会遇到这种情况,但嵌入式系统和错误恢复发现它很有用。
For a complete list of the shell builtins, use
man bash
. A builtin is a command that the shell has compiled inside the actual shell executable. That way, if for some reason you don't have theecho
command, you could still run it from the shell. This is handy for stripped down systems(such as embedded devices), or cases where you have accidentally erased sections of the harddrive.Usually, the builtin is more limited than the external executable, but it also does not require firing off another process, which you may not have enough resources to do. Again, most people don't run into that, but embedded systems and error recovery find it useful.
manbuiltin
将显示哪些命令是内置的,以及它们是如何执行的。从手册页:另外,在该手册页上,您还可以找到内置的其他进程。使用
man pwd
找出您的pwd
的特定版本。手册页是您的朋友:)
man builtin
will show you which commands are built-in, and how they are executed. From the man page:Also on that man page you can find out what other processes are built in. Use
man pwd
to find out your specific version ofpwd
.The man pages are your friends :)
正如 bash 主页中所述,您可以使用内置帮助来获取有关所有 bash 内置命令的信息。所以输入:
应该会给你一个完整的列表。使用内置函数的原因是它们更高效,因为调用它们不涉及跨越新进程。如果您不想使用给定命令的内置命令而是命令本身,则必须指定命令到 shell 的完整路径。例如
,而不只是
echo
As stated in the bash main-page, you can use the help built-in to get info about all the bash built-ins. So typing:
should give you a complete list. The reason for built-ins is that they are more efficient since invoking them does not involves spanning a new process. If you don't want to use the built-in for a given command but rather the command itself you must specify the full path of the command to the shell. e.g.
instead of just
echo