0

How can I write a script that prints the absolute path of the current working directory?

1 Answers1

6

If you want to get the directory from which the script is called you can use either the environment variable $PWD or the command pwd in most cases with command substitution like $(pwd).

If you want to get the actual location of the script you can use the variable $0 that contains the full script name (including the path).

Here is an example:

~$ cat /usr/local/bin/test-path.sh 
#/bin/sh
echo "${0}"     # full script name
echo "${0##*/}" # script name
echo "${0%/*}"  # script path
echo "$PWD"     # current working directory
echo "$(pwd)"   # current working directory
pwd             # current working directory
~$ test-path.sh
/usr/local/bin/test-path.sh
test-path.sh
/usr/local/bin
/home/pa4080
/home/pa4080
/home/pa4080

Here is one related topic: What kind of link to /bin/systemctl is /sbin/reboot?

pa4080
  • 29,831