1

Usually I source all the macros I have for the jobs run in a remote machine using this command:

 macros=$\my_directory

But I see someone uses a different way to get all the macros for submitting the jobs in a remote machine. He uses this command:

macros=$(dirname $(readlink -f $BASH_SOURCE))

Now I want to know how the $dirname has the advantages over giving the specific macro location. It would be great if you just explain to me regarding the sourcing the macro using $dirname

Dan D.
  • 73,243
  • 15
  • 104
  • 123
user193422
  • 69
  • 7
  • 1
    This gives you the directory of where the script is located, therefore it's easy to source other files locally close to your script and don't worry about specifying the correct path when the script is relocated. For instance `source $macros/some_script.sh` – NarūnasK Sep 02 '19 at 05:26
  • Do you mean that $BASH_SOURCE is giving the path of the directory of the bash script, and therefore I can have all the file location in that directory as well? – user193422 Sep 02 '19 at 05:31

1 Answers1

0

By using dirname you get the directory of where the script is located, therefore it's easy to source other files locally close to your script and don't worry about specifying the correct path each time the script bundle is relocated.

For instance if you have in your script source $macros/some_script.sh then it will not break when the bundle is located in the /usr/local/bin/ or /bin/ or ...

Regarding $BASH_SOURCE see: https://stackoverflow.com/a/35006505/2146346

NarūnasK
  • 4,564
  • 8
  • 50
  • 76