Bash script absolute path with OS X

后端 未结 15 1037
误落风尘
误落风尘 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-29 18:57

    I like this:

    #!/usr/bin/env bash
    function realpath() {
        local _X="$PWD"
        local _LNK=$1
        cd "$(dirname "$_LNK")"
        if [ -h "$_LNK" ]; then
            _LNK="$(readlink "$_LNK")"
            cd "$(dirname "$_LNK")"
        fi
        echo "$PWD/$(basename "$_LNK")"
        cd "$_X"
    }
    

提交回复
热议问题