简体   繁体   English

无论当前/工作目录如何,脚本目录路径

[英]Script directory path regardless of current/working directory

Is there a way to get the script directory regardless of change in current directory that occurred during script execution.有没有一种方法可以获取脚本目录,而不管脚本执行期间当前目录发生的变化。

echo $(dirname "$(readlink -f "$0")")
cd /tmp
echo $(dirname "$(readlink -f "$0")")
/home/user/test
/tmp

In the example above, I need /home/user/test both times, without storing it to a variable.在上面的示例中,我需要/home/user/test两次,而不将其存储到变量中。

This should do the job in your script:这应该在您的脚本中完成这项工作:

dir="$(readlink /proc/$PPID/cwd)"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM