sps*_*sps 33 bash echo cat shell-builtin
当我使用该type
命令来确定cat
是 shell 内置程序还是外部程序时,我得到以下输出:
-$ type cat
cat is hashed (/bin/cat)
-$
Run Code Online (Sandbox Code Playgroud)
这是否意味着这cat
是一个外部程序/bin/cat
?
我很困惑,因为当我检查下面的输出时,echo
我看到它既是built-in
一个程序,又是一个程序/bin/echo
-$ type echo
echo is a shell builtin
-$ which echo
/bin/echo
-$
Run Code Online (Sandbox Code Playgroud)
所以我不能使用/bin/cat
必然意味着外部程序的逻辑,因为 echo/bin/echo
仍然是内置的。
那我怎么知道是什么cat
?内置还是外置?
Joh*_*024 59
type
告诉你 shell 会使用什么。例如:
$ type echo
echo is a shell builtin
$ type /bin/echo
/bin/echo is /bin/echo
Run Code Online (Sandbox Code Playgroud)
这意味着如果在 bash 提示符下键入echo
,您将获得内置的。如果您指定路径,如 中所示/bin/echo
,您将获得外部命令。
which
,相比之下,它是一个外部程序,它对 shell 将做什么没有特别的了解。在类似 debian 的系统上,which
是一个 shell 脚本,它在 PATH 中搜索可执行文件。因此,即使 shell 使用内置文件,它也会为您提供外部可执行文件的名称。
如果命令仅作为内置命令可用,则which
不会返回任何内容:
$ type help
help is a shell builtin
$ which help
$
Run Code Online (Sandbox Code Playgroud)
现在,让我们看看cat
:
$ type cat
cat is hashed (/bin/cat)
$ which cat
/bin/cat
Run Code Online (Sandbox Code Playgroud)
cat
是外部可执行文件,而不是内置的 shell。
And*_*lla 46
cat is hashed (/bin/cat)
就像cat is /bin/cat
(也就是说,它是一个外部程序)。
不同之处在于您已经cat
在此会话中运行,因此 bash 已经在其中查找并将$PATH
结果位置存储在哈希表中,因此它不必在此会话中再次查找它。
要查看会话中已散列的所有命令,请运行 hash
$ hash
hits command
2 /usr/bin/sleep
3 /usr/bin/man
$ type sleep
sleep is hashed (/usr/bin/sleep)
$ type man
man is hashed (/usr/bin/man)
$ type ls
ls is /usr/bin/ls
$ type cat
cat is /usr/bin/cat
$ type echo
echo is a shell builtin
Run Code Online (Sandbox Code Playgroud)