How to expand relative paths in shell script

前端 未结 7 1256
春和景丽
春和景丽 2021-02-06 21:49

I am writing a script to set environment variables on linux 2.6 using bash. So the script contains commands like:

export SRC_DIR=..
export LIBPATH=${SRC_DIR}/lib         


        
7条回答
  •  不知归路
    2021-02-06 22:34

    Do this instead:

    export SRC_DIR=`pwd`;
    

    Update:

    Since you want path relative to the script's location on the filesystem, use this instead:

    export SRC_DIR=`dirname $0`
    

    Update2:

    Your script must be invoked directly and not as bash /path/to/script.sh or source foo.sh. Add a shebang line, add execute permissions and invoke the script directly.

提交回复
热议问题