Our directory structure looks like
/long/dir/name/bin/exec.sh
/long/dir/name/logs
/long/dir/name/input
/long/dir/name/output.
in exec.sh I want to retrieve the root directory (/long/dir/name in this case), store this in a variable so I can use it to refer to $DIR/output, $DIR/input etc.
I got as far as [exec.sh]:
#!/bin/sh
export DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | sed -e 's,/*[^/]\+/*$,,'
echo "$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )" | sed -e 's,/*[^/]\+/*$,,'
echo "My Dir: '$DIR'"
This outputs:
/long/dir/name <-- Which is what I want
My Dir: ''
What is going wrong when assigning it to the DIR variable?
$BASH_SOURCErather than$0makes more sense.