当我在 zsh 中运行以下脚本时,和都$0
给出$0:P
相同的输出:/Users/john/scripts/test123.sh
。
#!/bin/zsh
echo script\'s path: $0
echo absolute path: $0:P
echo parent folder: $0:h
echo filename: $0:t
echo filename w/o extension: $0:t:r
echo extension: $0:t:e
但为什么会这样?是不是因为:P
强制使用绝对路径(根据 zsh 文档),如果:P
不使用,路径应该是相对的,例如~/scripts/test123.sh
?
要运行该脚本,我首先要使其可执行:chmod a+rx test123.sh
然后双击它或使用命令行。操作系统是 macOS。