How can I write a script that prints the absolute path of the current working directory?
Asked
Active
Viewed 2,292 times
0
1 Answers
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?

BeastOfCaerbannog
- 14,585

pa4080
- 29,831
pwd
......... – Arkadiusz Drabczyk Aug 31 '22 at 11:55pwd
as a link to man pwd. – Jos Aug 31 '22 at 13:50echo "$PWD"
– glenn jackman Aug 31 '22 at 14:03pwd
wasn't first a built-in in most shells – Arkadiusz Drabczyk Sep 05 '22 at 17:42