How may I detect the name of the directory (or better yet the entire path) in which my shell script is run?
可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
回答1:
what shell? What operating system?
For starters try
man pwd
$PWD
回答2:
This, I believe, is the most portable way:
dir=$(cd -P -- "$(dirname -- "$0")" && pwd -P)
回答3:
This is not as trivial as it looks like. Check out this question and this
回答4:
alternative method
pid=$$
path=$(ps -eo pid,args| awk -vp=$pid '$1~p{print $3}')
case "$path" in
./* ) pwd;;
* ) echo $path;;
esac