2017年7月25日 星期二

Bash一行文:看script所在資料夾

寫一個Bash script,不管在哪邊執行的時候此腳本都可以印出所在的位置:
  • DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
上面這個一行文給的$DIR會儲存此腳本在系統上的完整路徑。即使是路徑裡面有symbolic link也沒有關係。但若此腳本是個symbolic link,那上述的方法就不行了。解法參考《Getting the source directory of a Bash script from within》裡面Dave Dopson的說明~
_EOF_

沒有留言:

張貼留言