确定shell程序中的相对path还是绝对path

正如标题所述,如果path是相对的或绝对的,我需要确定程序何时运行:

./program #relative dir/dir2/program #relative ~User/dir/dir2/program #absolute /home/User/dir/dir2/program #absolute 

这是我的testing用例。 我怎样才能在shell程序中做这件事?

或者更一般地说,如何检查在这种情况下$0的path是相对的还是绝对的?

Solutions Collecting From Web of "确定shell程序中的相对path还是绝对path"

 if [[ "$0" = /* ]] then : # Absolute path else : # Relative path fi 
 case "$directory" in /*) echo "absolute" ;; *) echo "relative" ;; esac 

任何$path通用解决方案,而不仅仅是$0

POSIX一班

 [ "$path" != "${path#/}" ] && echo "absolute" || echo "relative" 
 if [ ${path:0:1} == / ] then echo Absolute path else echo Non-absolute path fi