I have a script file, let's say abc.sh
. The full path is /home/path/to/script/abc.sh
. Now I am currently executing the script from /path/to/current
by executing this command -
sh /home/path/to/script/abc.sh
. I want this script to echo
it's location. The output should be /home/path/to/script
Asked
Active
Viewed 379 times
1

bosari
- 1,922
- 1
- 19
- 38
1 Answers
2
Use this script.
#!/bin/bash
current_location=$(pwd)
script_location=$(dirname $0)
if [ $script_location= '.' ]
then
script_location="$current_location"
fi
echo $script_location

Vishnu T S
- 3,476
- 2
- 23
- 39
-
Actually, OP is looking for: pwd – NVRM Jan 19 '18 at 17:39
-
1@Cryptopat I think no, he can use pwd if he is running the script from the directory where the script is stored..it won't work in another location – Vishnu T S Jan 19 '18 at 19:06