DEV Community

Cover image for How to Run a Bash Script From Anywhere on Your Apple Computer
Kyra
Kyra

Posted on • Originally published at simplykyra.com

How to Run a Bash Script From Anywhere on Your Apple Computer

I just wanted to start out by saying that this is an abridged version of my original published version from February 19, 2023 that you can find on my website here.


When you run a command in your terminal the computer looks through a list of directories where the file might reside and, if found, executes it. This special list of directories is stored in the PATH environment variable on your computer. If your shell goes through all of these paths, in order, and can’t find the file it then lets you know that it can’t be found before exiting. If there are two matching files it will run whichever one it comes across first.

To be able to run a script from anywhere on your computer the file holding the script needs to be in one of those directories stored in the PATH environment variable on your computer. So simply move your script to one of those directories!

But how do I know where that is?

To find out which directories are included in the PATH environment variable simply go to a terminal and type out echo $PATH. On my computer this looks like:

❯ echo $PATH
/Users/kyra/bin /usr/local/bin 
/System/Cryptexes/App/usr/bin /usr/bin /bin 
/usr/sbin /sbin /Library/Apple/usr/bin
Enter fullscreen mode Exit fullscreen mode

And from that I've chosen to put all of my scripts that I want to run from anywhere on my computer within the /Users/kyra/bin directory. Then I can call it from anywhere!

In my original post, that this is based on, I go into more information on the hows and whys this works including what to do if you want your script to live outside of the PATH environment variable or if you want to add a new directory to it's list. You can check this all out here.

I hope you're having a great day!

Top comments (0)