0

This question already has an answer here:

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.

marked as duplicate by Kusalananda bash May 6 '18 at 13:04

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

Browse other questions tagged or ask your own question.