如何将 ~/bin 添加到路径中?

我一直无法修改我的路径来添加崇高文本2。我已经添加了一个 ~/bin目录和 运行这个命令:

ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

Subl 链接出现在 ~/bin中。但是我需要将 ~/bin目录添加到我的路径中。我在这方面还是新手,我不知道我的路在哪里。我查看了一下,发现可能的文件是 .profile.bash_profile.bashrc

我没有 .bash_profile,我加了 .profile.bashrc

PATH=$PATH:~/bin/subl
export PATH

这样添加对吗? 如果是的话,我应该在哪里添加它?

当我 echo $PATH,我得到:

/Users/<username>/.rvm/gems/ruby-1.9.3-p194@rails3tutorial2ndEd/bin:/Users/<username>/.rvm/gems/ruby-1.9.3-p194@global/bin:/Users/<username>/.rvm/rubies/ruby-1.9.3-p194/bin:/Users/<username>/.rvm/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin

当我输入 subl时,我得到:

-bash: subl: command not found

谢谢!

P.s. 我之前安装了 Macports,它修改了我的 .profile文件。不知道这是否与它有任何关系-我现在不知道什么是默认的 .profile看起来像。

99519 次浏览

You don't need to do PATH=$PATH:~/bin/subl. Instead, PATH=$PATH:~/bin is sufficient. That way, you are telling the shell to look into ~/bin for binaries. With your command, you told the shell to look into the "folder" ~/bin/subl for binaries, which doesn't work. Furthermore, you don't need to add the commands in two files. Add them once in your .bashrc. I am not a bash expert, but I can recommend reading this blog post for further explanations of the different startup files.

I was just as new to this as you, which means I wasn't even sure how to read half the stuff related to modifying PATH. Eventually though I came upon a way to do it that doesn't require you to put subl into the .rvm/bin (since that is for something else) but in its appropriate directory, which is ~/bin.

Go to your terminal and type:

open -a Finder /usr/bin

This will open your Finder to the /usr/bin directory. Once you're there open a new Finder, go to Applications, right click on the Sublime Text icon and go to Show Package Contents, then to Contents, SharedSupport, bin. Copy the subl file and paste it into the other Finder showing /usr/bin

That's it! You should be set to use the command:

subl . (or subl file.name)

had the same challenge and ended up just creating a .bash_profile file, and adding the path statement directly there. Worked without incident. You may want to also check out Alvin Alexander's sample .bash_profile post (http://alvinalexander.com/blog/post/mac-os-x/sample-mac-osx-bashrc-terminal-startup-file) - I found a couple of other helpful commands that I'll be adding as well.

Update for zsh For some reason, "~/bin" in my path wasn't working when I recently switched from bash to zsh/prezto. I changed it to "/Users/myusername/bin" and it works fine once again. I'll be looking for a reason why and update when I find it. Please comment if you have a better solution.

I know this is an old post, but thought I'd document a solution for anyone else trying to follow the instructions given by sublimetext for working with sublime from the OSX command line verbatim. Update for Sublime text 3 Sublime Text 3 Documentation

1) Create a directory called "bin" in your home directory "~/"

mkdir ~/bin

2) Create a symbolic link to your sublime text 2 app in the new directory you just created

ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

Update for Sublime Text 3 app path is slightly different:

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

3) Follow Ryan Hoffman's instructions on how to easily add to the path in OSX: add to the path on Mac OSX Add the newly created "~/bin" to the path using his technique. Your /etc/paths file will look something like this when you're done (notice ~/bin at the end):

/usr/bin
/bin
/usr/sbin
/sbin
/usr/local/bin
~/bin

4) Open a new terminal window to start using subl from the command line.

following works with me. I have Sublime version 3 and posting it here if someone is looking for help:

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

You need to first create a symbolic link to /usr/bin. A symbolic link - or SymLink - is an alias or shortcut to a directory. Do as follow:

  1. First, make sure you are in your Home directory using the Terminal command line

    cd ~

  2. Create a symbolic link to your usr/bin directory.

    ln -s /usr/bin bin

    Where ln = create a link, -s = symbolic, followed by the [target diectory] and [name of link]

  3. Test your new link

    cd bin

    This should take you to your ~/bin - same as /usr/bin. The ~ indicates there's a long path hidden inside.

  4. Now, go back to your Home folder to install the subl command

    cd ~

  5. Install the Sublime Text 2 command line tool. I'll be using sudo to bypass any permission blocks.

    sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" ~/bin/subl

    You should be good to go! Test it by simply entering subl as your command line and it should launch Sublime Text 2 from Terminal.

Simple do it this on the terminal:

sudo ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/bin/subl

That is where my Sublime is stored, try to type the path to Sublime because your version may differ.

subl .

Should be working fine.

I had been struggling with this problem recently. I realized that the ~ isn't expanded to your home directory in the path. At least it wasn't for me.

This is what I did to make it work.

export PATH=$PATH:$HOME/bin

My subl was linked from the application directory into my ~/bin per some followup instructions I found for brew cask. Unfortunately the path was never updated.

Instread of all commented here. You need add symlink to sublime in /usr/local/bin. Its not require a root access. And don't need to create another bin directory.

For Sublime Text 2

ln -s /Applications/Sublime\ Text\ 2.app/Contents/SharedSupport/bin/subl /usr/local/bin/

For Sublime Text 3

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

I'd like to pose an alternate solution to this problem. Use a directory already in your path. Like this:

$ sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl

tl;dr

I ran into the same problem in Yosemite (OS X 10.10) where, in a fresh install of the OS, the ~/bin directory doesn't exist and isn't in your path. Yet there are lots of useful places already in your path you could place the symlink to Sublime.

For example here are the items currently in my path (use $ echo $PATH to get a list):

/Applications/Postgres.app/Contents/Versions/9.3/bin
/usr/local/bin
/usr/bin
/bin
/usr/sbin
/sbin:/usr/local/bin
/usr/local/mysql/bin

You can easily modify the script to use a location already in your path by changing the part that says ~/bin/subl to /usr/local/bin/subl

Thus, running the following command will accomplish your goal.

$ sudo ln -s "/Applications/Sublime Text 2.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl

It seems like in Yosemite the command is actually for Sublime 3, but I may be mistaken:

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

Here are the full instructions to do this (for Sublime Text 3):

  1. Please check your System Integrity Protection status by the following command:

    $ csrutil status
    

    If it's enabled, please follow these steps:

    1. Boot to the Recovery OS of OS X by restarting your machine and holding down Command + R at startup.

    2. Launch Terminal from the Utilities menu.

    3. Run the following command:

      $ csrutil disable
      $ reboot
      

    This is due to a security feature of OS X called System Integrity Protection, which will protect against unauthorised access to system locations and processes. So if this feature is enabled, you won't be able to modify the content of /usr/bin.

  2. Create a symlink from /usr/bin to ~/bin:

    $ sudo ln -s /usr/bin ~/bin
    
  3. Create a symlink from the Sublime Text 3 CLI tool to ~/bin:

    $ sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ~/bin
    
  4. Test it.

    $ subl .
    
echo $PATH

and use one of the path already in there. In this example "/usr/local/bin"

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

if the system return No such file or directory

sudo mkdir /usr/local/bin/
sudo ln -s "/Applications/Sublime Text.app/Contents/SharedSupport/bin/subl" /usr/local/bin/subl

test if it's working

subl

This is for Sublime Text 3.

On MacOS 11, this command worked for me to establish a symlink for Sublime Merge. The main difference seems to be calling ln with the -sv instead of -s:

ln -sv "/Applications/Sublime Merge.app/Contents/SharedSupport/bin/smerge" /usr/local/bin/smerge