BASH取得脚本所在路径

脚本所在路径通常来说,比pwd好用得多。以前,总是直接dirname $0,或者在perl里从__FILE__里取。今天又要写这个,看恶心了,想起bash自己就能做这种事情,但不记得语法了。于是google了一下”bash string manipulate”。

果然很好很强大:http://tldp.org/LDP/abs/html/string-manipulation.html

用得上的是这段:
${string%substring}
Deletes shortest match of $substring from back of $string.

于是${0%/*}把末尾’/’以后的全删掉就行了。
当然,这是相对于pwd的路径,不过已经够用了。