Skip to content

Instantly share code, notes, and snippets.

@itguy51
Last active August 29, 2015 14:03
Show Gist options
  • Save itguy51/fc8fed28ae3929eba379 to your computer and use it in GitHub Desktop.
Save itguy51/fc8fed28ae3929eba379 to your computer and use it in GitHub Desktop.
OS X Path Management
Paths For Mac OS X
Not long ago I found the existence of paths.d in OS X.
Essentially, $PATH is generated by appending the contents of each of the files in
/etc/paths.d/ to $PATH.
I decided that this could be incredibly useful for development, so I wrote a little
tool that will add, remove and list paths in this directory, so you don't have to
add them manually.
paths list
Lists the path names as well as the associated paths
paths remove <path name>
removes the named path
paths add <path name> <path>
adds a named path to the listed path
Just rename paths.sh to paths, chmod +x paths then copy it to /usr/bin
The script does not rely at all on its name, so if you wish to rename it,
please feel free
- Josh
P.S. I know the script isn't terribly efficient, but does it really REALLY
need to execute faster than it does already?
P.P.S. If you use my scripts often, fund my coffee. (Bitcoin: 1NMkzf4ZwtwAXW9pW5MwABn71vg5acD9q3)
====== MIT License ======
Copyright (C) 2014 Josh Pruim
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#!/bin/sh
#paths
#For Mac OS X
#MIT Licensed
#2014 Josh Pruim
#
#This adds an entry to /etc/paths.d
#Accepts 1/2/3 parameters
#paths add <path name> <path>
#paths remove <path name>
#paths list
#paths add node /usr/bin/node
#the above would add a file called node in /etc/paths.d with
#a pointer to /usr/bin/node/
if [ "$1" = "list" ]; then
if [ "`ls -l /etc/paths.d | grep -v ^l | wc -l`" -gt "0" ]; then
for filename in /etc/paths.d/*; do
if [ "$filename" != "/etc.paths.d/*" ]; then
echo "`echo $filename | sed "s@/etc/paths.d/@@"` - `cat $filename`"
fi
done
fi
fi
if [ "$1" = "add" ]; then
if [ "$#" -ne 3 ]; then
echo "Illegal number of parameters"
exit 1
fi
sudo sh -c "echo \"$3\" > /etc/paths.d/$2"
fi
if [ "$1" = "remove" ]; then
if [ "$#" -ne 2 ]; then
echo "Illegal number of parameters"
exit 2
fi
sudo sh -c "rm /etc/paths.d/$2"
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment