-1

I have a shell program in a directory (ie dir1/dothis.sh) - works fine when I cd to that directory and ./dothis.sh

if I created a ln to that directoy with a new name - dir2 and do dir2/dothis.sh it would execute but it thinks the current execution path is the new dir where dir2 is pointing to at

in dothis.sh - how do I find where dothis.sh actually located? The problem I have is that the dir1/dothis.sh can be relocated from system to system so there is no warranty where dir1/dothis.sh can be hard code

3 Answers3

1

Use the bash built-in

#!/bin/bash
echo "Current path: $PWD"
koola
  • 1,616
  • 1
  • 13
  • 15
0

try this:

#!/bin/bash
echo $0
a=`pwd`
echo $a
b=$a"/"$0
echo `dirname $b`
Marcus
  • 6,701
  • 4
  • 19
  • 28
0

How about

dirname $(readlink -f $0)

It will also resolve symbolic link if any...

anishsane
  • 20,270
  • 5
  • 40
  • 73