首页 > 代码库 > Get Script Path in Shell

Get Script Path in Shell

#!/usr/bin/bash

dir_old=$(pwd)
absolute_script_path=$(cd $(dirname $0) && pwd)
relative_script_path=$(dirname $0)
wrong_absolute_script_path=$dir_old/$relative_script_path

echo "Current PATH: $dir_old"
echo "absolute_script_path is: $absolute_script_path"
echo "relative_script_path is: $relative_script_path"
echo "This path might be wrong: $wrong_absolute_script_path"

# Fail if use absolute path to invoke script
cd $wrong_absolute_script_path && pwd

Get Script Path in Shell