如果是符号链接,则调用“获取目录Bash脚本”

问题描述 投票:0回答:1

假设我在program中存储了一个名为/home/user/program.的bash脚本,但是随后我使用ln -s /home/user/program /usr/bin/program创建了一个指向bash脚本的符号链接。然后我cd /usr/home/anotherdirectory,然后运行program。我如何获得bash脚本以打印/usr/home/anotherdirectory来告诉它从何处调用?

我尝试过echo $PWD,它只打印出/ usr / bin

bash directory symlink
1个回答
1
投票

这应该完成您脚本中的工作:

dirname $(readlink -e "$0")

来自man readlink

[-e:通过递归跟随给定名称的每个组件中的每个符号链接来规范化,所有组件必须存在]]

© www.soinside.com 2019 - 2024. All rights reserved.