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

1

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!

DevCycle image

Ship Faster, Stay Flexible.

DevCycle is the first feature flag platform with OpenFeature built-in to every open source SDK, designed to help developers ship faster while avoiding vendor-lock in.

Start shipping

Top comments (0)

Feature flag article image

Create a feature flag in your IDE in 5 minutes with LaunchDarkly’s MCP server 🏁

How to create, evaluate, and modify flags from within your IDE or AI client using natural language with LaunchDarkly's new MCP server. Follow along with this tutorial for step by step instructions.

Read full post

World's Largest Hackathon Awards Ceremony

Join us for the World’s Largest Hackathon Award Ceremony as we announce the winners and recognize the amazing work from this community!

Tune in to the full event

DEV is partnering to bring live events to the community. Join us or dismiss this billboard if you're not interested. ❤️