Kapil Bhandari Kapil Bhandari - 3 months ago 12
Bash Question

MAC : bash script which invokes a Python script , both these scripts are in the same directory

I have written a bash script in MAC , which is placed in a directory say xyz.
Now i have a python file in the same directory xyz

We can also say that this python script will always be in the directory where this bash script is

So i want this bash script to be general

/Library/Frameworks/Python.framework/Versions/3.5/bin/python3.5 /Users/labuser/Desktop/Installer/OpenURL_GenericNotification.py Some Arguments

So i want to replace
/Users/labuser/Desktop/Installer/
so that from wherever this script is run python script is automatically calculated by some system variable like $cd
/Users/labuser/Desktop/Installer/ this like $cd in windows not sure how in MAC
Any comments on how to go about this script ??

Answer

In a Bash script, the path to the script is stored in the $0 variable.

The dirname command returns the directory portion of a path (filename removed).

You can use the dirname with $0 to get the script's base directory, and cd to that path, and then run the Python script in the current directory:

cd "$(dirname "$0")"

./OpenURL_GenericNotification.py
Comments