Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get absolute path name of shell script on MacOS?

Tags:

bash

shell

sh

macos

readlink -f does not exist on MacOS. The only working solution for Mac OS I managed to find on the net goes like this:

if [[ $(echo $0 | awk '/^\//') == $0 ]]; then     ABSPATH=$(dirname $0) else     ABSPATH=$PWD/$(dirname $0) fi 

Can anyone suggest anything more elegant to this seemingly trivial task?

like image 714
Ivan Balashov Avatar asked Apr 22 '11 14:04

Ivan Balashov


1 Answers

Another (also rather ugly) option:

ABSPATH=$(cd "$(dirname "$0")"; pwd -P) 

From pwd man page,

-P      Display the physical current working directory (all symbolic links resolved). 
like image 102
Gordon Davisson Avatar answered Oct 11 '22 13:10

Gordon Davisson