shell 中内置的工具

发布于 2024-12-20 15:35:49 字数 648 浏览 2 评论 0原文

我想知道 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

落在眉间の轻吻 2024-12-27 15:35:49

有什么办法可以找出内置的东西吗?

help将为您提供完整的列表。您可以使用内置命令作为参数运行 help 来获取更详细的信息。 info "(bash) Shell 内置命令" 将显示所有内置命令的 Bash 手册。

bin 文件夹(上图)中的 pwd 与内置的 pwd 相同吗?

不,它们是完全不同的:

$ builtin pwd --help
bash: pwd: --: invalid option
pwd: usage: pwd [-LP]

$ /bin/pwd --help
Usage: /bin/pwd [OPTION]...
Print the full filename of the current working directory.

  -L, --logical   use PWD from environment, even if it contains symlinks
  -P, --physical  avoid all symlinks
      --help     display this help and exit
      --version  output version information and exit

NOTE: your shell may have its own version of pwd, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

Report bugs to <[email protected]>.

为什么首先要内置这些东西?它们是否经过专门调整以适合外壳,或者只是为了可以在内部调用它们,这样它们就不需要新的进程?

手册中写道:“内置命令对于实现使用单独的实用程序不可能或不方便获得的功能是必要的。”很难让像 cd 这样的命令在外部工作,因为它会影响 shell 的状态。当然,复制 pwdtrue 的行为很容易,但是 POSIX 标准 要求它们是内置的

我设法用 pwd &ps 捕获了 pwd。这是一种规避还是它们是单独的过程?

运行 builtin & 将导致 Bash 在后台运行子 shell。您可以通过执行 read & 轻松看到这一点,因为 read 会等待直到有输入。

Is there any way to find out what stuff are built in?

help will get you a complete list. You can run help 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.

Is the pwd in the bin folder (above) the same pwd that is built in?

No, they are completely different:

$ builtin pwd --help
bash: pwd: --: invalid option
pwd: usage: pwd [-LP]

$ /bin/pwd --help
Usage: /bin/pwd [OPTION]...
Print the full filename of the current working directory.

  -L, --logical   use PWD from environment, even if it contains symlinks
  -P, --physical  avoid all symlinks
      --help     display this help and exit
      --version  output version information and exit

NOTE: your shell may have its own version of pwd, which usually supersedes
the version described here.  Please refer to your shell's documentation
for details about the options it supports.

Report bugs to <[email protected]>.

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?

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 of pwd or true, but the POSIX standard requires that they are built-ins.

I managed to catch a pwd with pwd & and ps. Is this a circumvention or are they separate processes?

Running builtin & will cause Bash to run a subshell in the background. You can see this easily by doing read &, since read waits until it has input.

樱花落人离去 2024-12-27 15:35:49

为了回答你的第一个问题,我发现如果我(在我的 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

星星的軌跡 2024-12-27 15:35:49

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).

又爬满兰若 2024-12-27 15:35:49

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.

whichcommand 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, but bash in particular is usually smart enough to still fork 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 circumvents alias and the like.) — This is rarely useful with bash, 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.

甜柠檬 2024-12-27 15:35:49

有关 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 the echo 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.

鱼忆七猫命九 2024-12-27 15:35:49

manbuiltin 将显示哪些命令是内置的,以及它们是如何执行的。从手册页:

Shell 内置命令是可以在正在运行的 shell 进程中执行的命令。

另外,在该手册页上,您还可以找到内置的其他进程。使用 man pwd 找出您的 pwd 的特定版本。

手册页是您的朋友:)

man builtin will show you which commands are built-in, and how they are executed. From the man page:

Shell builtin commands are commands that can be executed within the running shell's process.

Also on that man page you can find out what other processes are built in. Use man pwd to find out your specific version of pwd.

The man pages are your friends :)

断桥再见 2024-12-27 15:35:49

正如 bash 主页中所述,您可以使用内置帮助来获取有关所有 bash 内置命令的信息。所以输入:

help

应该会给你一个完整的列表。使用内置函数的原因是它们更高效,因为调用它们不涉及跨越新进程。如果您不想使用给定命令的内置命令而是命令本身,则必须指定命令到 shell 的完整路径。例如

/bin/echo

,而不只是 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:

help

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.

/bin/echo

instead of just echo

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文