如果我有一个shell脚本,我使用“../”获取父文件夹,我可以将它扩展到它的绝对路径吗?
You want readlink -f
.
$ cd /tmp
$ readlink -f ..
/
Use realpath
$ realpath ..
/home
While you can often get the full path of a directory using stat
, you can also use pwd -P
... however, it isn't much help unless you actually cd
to the directory you are interested in.
To get around this limitation without actually changing the current working directory, I've found it fine to just run that cd
in a sub-shell , as in:
THIS=`dirname $0` # Which is often "."
PARENT=$(
cd $THIS # At this point, we are sure we're in script's directory
dirname `pwd -P`
)
echo "PARENT=$PARENT"
When using in cmd line:
dirname $(pwd)
When using in shell script:
PROJ_DIR=$(dirname $(pwd))
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.