ThinkChat🤖让你学习和工作更高效,注册即送10W Token,即刻开启你的AI之旅 广告
` `在运行shell脚本时,我们有时候需要获取shell的绝对路径,很多是使用`dirname $0`,但是这不一定能能够获得绝对路径,这取决于你当前终端所在的路径。 ` `比如我一下路径中run文件的路径: ![](https://img.kancloud.cn/5f/b8/5fb82fd0e6d656dd589b0cda3d2baf32_358x239.png) ` `如果我们需要获得完全的绝对路径,可以使用`readlink`命令, ``` 用法:readlink [选项]... 文件... 输出符号链接值或权威文件名。 -f, --canonicalize 递归跟随给出文件名的所有符号链接以标准化, 除最后一个外所有组件必须存在 -e, --canonicalize-existing 递归跟随给出文件名的所有符号链接以标准化, 所有组件都必须存在 -m, --canonicalize-missing canonicalize by following every symlink in every component of the given name recursively, without requirements on components existence -n, --no-newline do not output the trailing delimiter -q, --quiet, -s, --silent suppress most error messages (on by default) -v, --verbose report error messages -z, --zero end each output line with NUL, not newline --help 显示此帮助信息并退出 --version 显示版本信息并退出 ``` ![](https://img.kancloud.cn/51/5e/515e0b3fe9f7177c10a12d54959f13cd_359x240.png) ` `在脚本中可以使这样: ```[shell] #!/bin/bash dir=`dirname $0;pwd` dir1=`pwd` dir2=$(readlink -f $0) echo $dir2 ```