Execute Shell Scripts From any Directory

I previously mentioned that I use shell scripts to make my work more efficient. I used to keep scripts scattered around my home directory, but now keep them organized in a single directory. Feel free to skip the initial step below if you’re the disorganized type.

Step Zero

Put all scripts into the same directory. I keep mine in ~/bin. I used to use ~/.bin to keep them hidden from view, but found that I tended to forget about the directory and kept old scripts that I didn’t use anymore.

Step One

Make the script executable. Let’s say I have a script named “banana” that I want to be able to use from any directory on my machine:

Chmod +x banana

Now, banana is executable. I can verify the new permissions by running ls -l or by checking for the asterisk (*) after the script name:


Step Two

I add the bin directory to the known paths so that scripts there can be run from anywhere. I define the path in my ~/.bash_profile. The .bash_profile file may already have some paths defined already. If it does, the script directory can be added to the line. If it doesn’t, I add both the script directory and the current paths ($PATH) to be sure the new path(s) don’t override the old:

export PATH="~/bin:$PATH"

Although I could add multiple paths after the colon and add as many paths as I need in one line, I keep my .bash_profile readable by adding a separate export… line for each path. I have separate PATHs for Golang, Python, Git, etc, and I prefer to keep each on a separate line.


Published by josephmidura

I am a software developer in Austin.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: