大网数据广告活动
服务器运维问答

Get on zsh the same result you get when executing `type -t` on bash?

· 52CE 我爱测

I have shell script that uses type -t , but since apparently it was originally written for bash and on this machine I on zsh , it's not working. I see bash docs that type -t does: If the -t option is used, type prints a single wo…

问题原文

原帖链接 · yivi · CC BY-SA 3.0

I have shell script that uses type -t, but since apparently it was originally written for bash and on this machine I on zsh, it's not working.

I see bash docs that type -t does:

If the -t option is used, type prints a single word which is one of ‘alias’, ‘function’, ‘builtin’, ‘file’ or ‘keyword’, if name is an alias, shell function, shell builtin, disk file, or shell reserved word, respectively. If the name is not found, then nothing is printed, and type returns a failure status.

But in zsh, type doesn't accept the -t option, and it actually aliases to whence:

type [ -fpam ] name ... Same as whence -v.

And whence:

For each name, indicate how it would be interpreted if used as a command name. The '-v' flag produces a more verbose report. The '-c' flag prints the results in a csh-like format and takes precedence over '-v'. The '-f' flag causes the contents of a shell function to be displayed, which would otherwise not happen unless the '-c' flag were used. The '-p' flag does a path search for name even if it is an alias, reserved word, shell function or builtin. The '-a' flag does a search for all occurrences of name throughout the command path. With the '-m' flag, the arguments are taken as patterns (which should be quoted), and the information is displayed for each command matching one of these patterns.

Which does not included the desired output. A way to get the en equivalent result on zsh? Changing shells is not an option on this machine.

已采纳解答

原帖链接 · J.M. Robles · CC BY-SA 3.0

As outputs from whence -w in zsh and type -t in bash are something similar (the following table is made from personal tests, so I recommend take it with care)

type -t    whence -w
alias      alias
function   function
builtin    builtin
file       command
keyword    reserved
?          hashed
?          none

It seems not too difficult to create a script in zsh that mimics the behaviour of type -t in bash. You could even, e.g. by means of alias, replace the builtin type in zsh with your command in zsh

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2017-10-19)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。