OSX How do I have a Shell script change directory to the one the script is in? OSX How do I have a Shell script change directory to the one the script is in? shell shell

OSX How do I have a Shell script change directory to the one the script is in?


A standard idiom for this in shell scripts is dirname $0. The $0 variable is the path to the script that was executed, and the dirname command takes a path and strips off the last component to leave the path to the containing directory

cd "`dirname $0`"


Just wanted to weigh in here. I've seen some people with this problem. If you are JUST on OSX and making your own scripts. This will do the trick :) kind of a hack, but works like a charm.

#! /bin/bash sudo /Applications/XAMPP/xamppfiles/xampp startapacheopen /Applications/XAMPP/htdocs


#!/bin/bashDIR=$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )cd "${DIR}"...the rest of your script...

Credits to Ian C. on AskDifferent: https://apple.stackexchange.com/a/179064