类Unix系统中,非可执行命令的which等效工具是什么?
nvm in Unix-like Systems Great question! When which returns nothing for a command you know works (like nvm), that’s because it’s not a standalone executable file—instead, it’s likely a shell built-in, alias, or shell function. Here are the go-to tools to track down these types of commands, acting as equivalents (and often improvements) to which:
1. type Command
This is the most straightforward tool for this job. It tells you exactly what kind of command you’re dealing with and even shows its definition if it’s an alias or function.
- Run
type nvmand you’ll get output like:nvm is a function nvm () { # ... full function definition here ... } - For aliases, it might show something like
nvm is aliased to 'nvm use default' - For shell built-ins (like
cd), it’ll saycd is a shell builtin
2. command -v (POSIX-Compliant)
If you need a tool that works consistently across all POSIX-compliant shells (not just bash/zsh), command -v is your friend. It outputs the command’s location or definition just like type, but in a more script-friendly format.
- Example:
command -v nvmwill return the path if it’s an executable, or the alias/function definition if it’s not.
3. alias Command
If you suspect the command is an alias, use this to list all aliases or check a specific one:
- List all aliases:
alias - Check a specific alias:
alias nvm(will return the alias definition if it exists)
4. declare -f (Bash) / functions (Zsh)
To see the full code of a shell function (like nvm, which is typically implemented as a function), use these commands:
- In bash:
declare -f nvm(prints the entire function body) - In zsh:
functions nvm(does the same as above for zsh functions)
5. help Command
For shell built-in commands (like echo or cd), help will confirm it’s a built-in and show its usage:
- Example:
help cdwill display the built-in command’s documentation.
Quick Tip
Start with type first—it’s the most informative for interactive use. If you’re writing a script, stick with command -v for portability.
内容的提问来源于stack exchange,提问作者Martin Braun

