How do I get the absolute directory of a file in bash?

前端 未结 7 1801
臣服心动
臣服心动 2020-12-04 14:03

I have written a bash script that takes an input file as an argument and reads it.
This file contains some paths (relative to its location) to additional files used.

7条回答
  •  孤街浪徒
    2020-12-04 15:01

    This will work for both file and folder:

    absPath(){
        if [[ -d "$1" ]]; then
            cd "$1"
            echo "$(pwd -P)"
        else 
            cd "$(dirname "$1")"
            echo "$(pwd -P)/$(basename "$1")"
        fi
    }
    

提交回复
热议问题