hug*_*omg 23 linux unix command-line
例如,如果ls作为输入传递,它应该告诉我/bin/ls如果ls在命令行上运行,它将运行。
Jde*_*eBP 26
只有内置的 shell 才能正确地告诉人们 shell 将对给定的命令名称执行什么操作,因为只有内置函数才能完全了解别名、shell 函数、其他内置函数等。请记住:并非所有命令首先都对应于可执行文件。
对于Bourne Again Shell的,bash,内置的是type命令:
$ type '['
[ is a shell builtin
Run Code Online (Sandbox Code Playgroud)对于 Fish shell,fish ,type内置函数的工作方式与 bash 类似。要获取可执行文件的路径,请使用command -v:
$ type cat
cat is /bin/cat
$ command -v cat
/bin/cat
Run Code Online (Sandbox Code Playgroud)对于 Korn Shell,ksh,内置whence命令 -type最初设置为普通别名 forwhence -v和command内置-v选项等效于whence:
$ whence -v ls
ls is a tracked alias for /bin/ls
Run Code Online (Sandbox Code Playgroud)对于 Z Shell,zsh,内置是whence命令,command内置-v选项等效于whence和内置type, which, 和where等效于whence选项-v, -c, 和-ca。
$ whence ls
/bin/ls
Run Code Online (Sandbox Code Playgroud)对于 TC Shell, tcsh,内置which命令 - 不要与任何具有该名称的外部命令混淆:
> which ls
ls: aliased to ls-F
> which \ls
/bin/ls
Run Code Online (Sandbox Code Playgroud)NPE*_*NPE 14
您可以which为此使用:
aix@aix:~$ which ls
/bin/ls
Run Code Online (Sandbox Code Playgroud)
它通过搜索PATH与参数名称匹配的可执行文件来工作。请注意,它不适用于 shell 别名:
aix@aix:~$ alias listdir=/bin/ls
aix@aix:~$ listdir /
bin dev initrd.img lib32 media proc selinux tmp vmlinuz
...
aix@aix:~$ which listdir
aix@aix:~$
Run Code Online (Sandbox Code Playgroud)
type,但是,确实有效:
aix@aix:~$ type listdir
listdir is aliased to `/bin/ls'
Run Code Online (Sandbox Code Playgroud)
which不不(一定)返回的可执行文件。它返回它在 $PATH 中找到的第一个匹配文件名(或使用时多个类似的命名文件which -a)...实际的可执行文件可能与多个链接相距甚远。
which locate/usr/bin/locatefile $(which locate)/usr/bin/locate: symbolic link to /etc/alternatives/locate' 查找实际可执行文件的命令是readlink -e,
(与 结合使用which)
readlink -e $(which locate)/usr/bin/mlocate 查看所有中间链接:
f="$(which locate)" # find name in $PATH
printf "# %s\n" "$f"
while f="$(readlink "$f")" ;do # follow links to executable
printf "# %s\n" "$f"
done
# /usr/bin/locate
# /etc/alternatives/locate
# /usr/bin/mlocate
Run Code Online (Sandbox Code Playgroud)