Creating a symbolic link in /bin is not working

I've tried three different ways to create a symbolic link to Sublime Text on my Mac and, after restarting terminal each time, it still keeps telling me command not found. I'm using a Mac with Yosemite (OSX Yosemite, 10.10). How can I create a symbolic link so I can open Sublime with the sub command?

One

ln -s 'Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl' /bin/sub

Two

ln -s "Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl" /bin/sub

Three (no quotes)

ln -s Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /bin/sub

Solution 1:

You're missing the '/' at the beginning of your app and sudo as /bin is owned by root and you can't actually write to it as a normal user. As you currently have it, it would only work if your present directory was the system root. This assumes of course that the Sublime Text app is in your main Applications folder.

Try this…

sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /bin/sub

One afterthought: It's considered bad practice to change the content of /bin and /usr/bin because any upcoming OS X upgrade may wipe the content there. /usr/local/bin is the usual place to install user-provided binaries in, so

[[ -d /usr/local/bin ]] || sudo mkdir /usr/local/bin
sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl /usr/local/bin/sub

might be the better option.

Or (as you probably never need to run Sublime Text from within a shell script), simply define a bash alias

alias sub=/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl

which should be added to your bash startup file to make it stick

echo "alias sub=/Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl" >> ~/.bashrc

Solution 2:

Late to the party. I encountered the same issue when setting up my Mac and tried a few things. Here is what worked for me.

ln -sv "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl

For more information, go to this github repo.