How can I write a script that prints the absolute path of the current working directory?
Asked
Active
Viewed 2,700 times
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
- 16,703
pa4080
- 30,621