0

I have a script to verify the positional parameters it receives

$ cat myscript
#! /bin/bash
echo $0
echo $1
echo $2

When I run it via different ways

$  ./myscript   
./myscript

$ bash ./myscript 
./myscript

$ source myscript
/bin/bash

Suppose I have to write another script which needs to get the pathname to the parent directory of the script:

#! /bin/bash

echo $(dirname "$(readlink -f "$0")")

and I have to run it by source it. Then it won't work, because $0 is /bin/bash. I wonder how I can get the pathname to the parent directory of the script within itself, when source it? Thanks.

Tim
  • 101,790

0 Answers0