Bash script absolute path with OS X

后端 未结 15 1032
误落风尘
误落风尘 2020-11-29 18:20

I am trying to obtain the absolute path to the currently running script on OS X.

I saw many replies going for readlink -f $0. However since OS X\'s

15条回答
  •  一向
    一向 (楼主)
    2020-11-29 19:02

    abs_path () {    
       echo "$(cd $(dirname "$1");pwd)/$(basename "$1")"
    }
    

    dirname will give the directory name of /path/to/file, i.e. /path/to.

    cd /path/to; pwd ensures that the path is absolute.

    basename will give just the filename in /path/to/file, i.e.file.

提交回复
热议问题