0

I am writing a shell script which runs pwd command and uses its output for some others tasks performed by the script. The script is working totally fine when I go to the particular directory and run it. But when I am trying to run the script from some other location by giving the full path of the script, I am not getting the desired output as pwd command gives current directory's path. How can I solve this issue? How can I write something that will hold correct irrespective of where I run the script from?
The same issue is being faced when I am using ..to get to previous directory. I want the script to take path with respect to its location instead of path where the script is being run. Please let me know if there are some other details required.

user3379410
  • 176
  • 1
  • 11

1 Answers1

0

You can use $0 to extract script and then combine it with pwd

$ cat abc.sh
echo $0

DIR1=$(dirname `pwd`/$0)
echo $DIR1

DIR2="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
echo $DIR2

There are other ways to achieve it in all cases:

Community
  • 1
  • 1
tuxdna
  • 8,257
  • 4
  • 43
  • 61