<p>I'm converting some Windows batch files to Unix scripts using sh. I have problems because some behavior is dependent on the %~dp0 macro available in batch files.</p>

<p>Is there any sh equivalent to this? Any way to obtain the directory where the executing script lives?</p>

Yes, you can! It's in the arguments. :)
look at
```
```
${0}
```
```
combining that with
```
```
{$var%Pattern}
Remove from $var  the shortest part of $Pattern that matches the back end of $var.
```
```
what you want is just
```
```
${0%/*}
```
```
I recommend the
Advanced Bash Scripting Guide
(that is also where the above information is from).
Especiall the part on
Converting DOS Batch Files to Shell Scripts
might be useful for you. :)
If I have misunderstood you, you may have to combine that with the output of "pwd". Since it only contains the path the script was called with!
Try the following script:
```
```
#!/bin/bash
called_path=${0%/*}
stripped=${called_path#[^/]*}
real_path=`pwd`$stripped
echo "called path: $called_path"
echo "stripped: $stripped"
echo "pwd: `pwd`"
echo "real path: $real_path
```
```
This needs some work though.
I recommend using Dave Webb's approach unless that is impossible.