Bash script absolute path with OS X

后端 未结 15 1035
误落风尘
误落风尘 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 18:52

    This seems to work for OSX, doesnt require any binaries, and was pulled from here

    function normpath() {
      # Remove all /./ sequences.
      local path=${1//\/.\//\/}
    
      # Remove dir/.. sequences.
      while [[ $path =~ ([^/][^/]*/\.\./) ]]; do
        path=${path/${BASH_REMATCH[0]}/}
      done
      echo $path
    }
    

提交回复
热议问题