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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10767738/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 22:09:09  来源:igfitidea点击:

How do I add ~/bin to my path?

macosbashsublimetext

提问by Graeme

I've been having trouble modifying my path to add Sublime Text 2. I've added a ~/bindirectory and run this command:

我在修改路径以添加 Sublime Text 2 时遇到问题。我添加了一个~/bin目录并运行以下命令

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

The subl link appears in ~/bin. But I need to add the ~/bindirectory to my path. I'm fairly new at this, and I don't know where my path is. I've looked around, and found that the likely files are either .profile, .bash_profileor .bashrc

subl 链接出现在~/bin. 但是我需要将该~/bin目录添加到我的路径中。我在这方面相当新,我不知道我的道路在哪里。我环顾四周,发现可能的文件是.profile,.bash_profile.bashrc

I don't have a .bash_profile. To .profileand .bashrcI added

我没有.bash_profile。要.profile.bashrc我说

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

Is that the right thing to add? And if so, where should I add it?

添加这个是正确的吗?如果是这样,我应该在哪里添加它?

When I echo $PATH, I get:

当我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

When I type subl, I get:

当我输入时subl,我得到:

-bash: subl: command not found

Thanks!

谢谢!

p.s. I had previously installed Macports, which modified my .profilefile. Not sure if this has anything to do with it - I now don't know what the default .profilelooks like.

ps我之前安装了Macports,它修改了我的.profile文件。不确定这是否与它有关 - 我现在不知道默认值是什么.profile样的。

回答by dgamboa

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.

我和你一样对此很陌生,这意味着我什至不确定如何阅读与修改 PATH 相关的一半内容。最终,尽管我找到了一种方法,它不需要您将 subl 放入 .rvm/bin(因为这是用于其他内容),而是放入其适当的目录中,即 ~/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

这将打开您的 Finder 到 /usr/bin 目录。在那里打开一个新的 Finder,转到 Applications,右键单击 Sublime Text 图标并转到 Show Package Contents,然后转到 Contents、SharedSupport、bin。复制 subl 文件并将其粘贴到另一个显示 /usr/bin 的 Finder 中

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

就是这样!您应该设置为使用以下命令:

subl . (or subl file.name)

回答by Igor A. Melekhine

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.

Instread 所有评论在这里。您需要在/usr/local/bin. 它不需要root访问权限。并且不需要创建另一个 bin 目录。

For Sublime Text 2

对于 Sublime Text 2

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

For Sublime Text 3

对于崇高的文本 3

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

回答by Shawn

Update for zshFor 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.

更新 zsh出于某种原因,当我最近从 bash 切换到 zsh/prezto 时,我路径中的“~/bin”不起作用。我将其更改为“/Users/myusername/bin”,它再次正常工作。我会寻找原因并在找到时进行更新。如果您有更好的解决方案,请发表评论。

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

我知道这是一篇旧帖子,但我想我会为其他任何试图按照sublimetext 给出说明从 OSX 命令行逐字处理 sublime 的人记录解决方案。更新崇高的文本3崇高的文本3文档

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

1)在你的主目录“~/”中创建一个名为“bin”的目录

mkdir ~/bin

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

2)在您刚刚创建的新目录中创建一个指向您的 sublime text 2 应用程序的符号链接

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

Update for Sublime Text 3app path is slightly different:

Sublime Text 3应用程序路径的更新略有不同:

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 OSXAdd 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):

3) 按照 Ryan Hoffman 关于如何在 OSX 中轻松添加到路径的说明进行操作:在 Mac OSX 上添加到路径 使用他的技术将新创建的“~/bin”添加到路径中。完成后,您的 /etc/paths 文件将如下所示(注意 ~/bin 最后):

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

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

4) 打开一个新的终端窗口,从命令行开始使用 subl。

回答by Gnosophilon

You don't need to do PATH=$PATH:~/bin/subl. Instead, PATH=$PATH:~/binis sufficient. That way, you are telling the shell to look into ~/binfor binaries. With your command, you told the shell to look into the "folder" ~/bin/sublfor 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 bashexpert, but I can recommend reading this blog postfor further explanations of the different startup files.

你不需要做PATH=$PATH:~/bin/subl。相反,PATH=$PATH:~/bin就足够了。这样,您就告诉 shell 查找~/bin二进制文件。使用您的命令,您告诉 shell 查看~/bin/subl二进制文件的“文件夹” ,这不起作用。此外,您不需要在两个文件中添加命令。在您的.bashrc. 我不是bash专家,但我建议您阅读这篇博文,以进一步了解不同的启动文件。

回答by Greg Hemphill

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

tl;博士

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.

我在优胜美地 (OS X 10.10) 中遇到了同样的问题,在全新安装的操作系统中, ~/bin 目录不存在并且不在您的路径中。然而,您的路径中已经有很多有用的地方,您可以将符号链接放置到 Sublime。

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

例如,这里是当前在我的路径中的项目(用于$ echo $PATH获取列表):

/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/sublto /usr/local/bin/subl

您可以通过更改表示~/bin/subl为的部分轻松修改脚本以使用路径中已有的位置/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

回答by mpaiva

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:

您需要首先创建一个指向/usr/bin. 符号链接 - 或 SymLink - 是目录的别名或快捷方式。请按以下步骤操作:

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

    cd ~

  2. Create a symbolic link to your usr/bindirectory.

    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 sublcommand

    cd ~

  5. Install the Sublime Text 2 command line tool. I'll be using sudoto 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 sublas your command line and it should launch Sublime Text 2 from Terminal.

  1. 首先,使用终端命令行确保您位于主目录中

    cd ~

  2. 创建指向您的usr/bin目录的符号链接。

    ln -s /usr/bin bin

    其中 ln= 创建链接,-s= 符号,后跟[target diectory][name of link]

  3. 测试您的新链接

    cd bin

    这应该带你到你的~/bin- 一样/usr/bin。该~指示还有很长的路径隐藏在里面。

  4. 现在,返回您的主文件夹以安装subl命令

    cd ~

  5. 安装 Sublime Text 2 命令行工具。我将sudo用来绕过任何权限块。

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

    你应该很高兴去!通过简单地subl作为命令行输入来测试它,它应该从终端启动 Sublime Text 2。

回答by cassioscabral

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.

那是我的 Sublime 的存储位置,请尝试输入 Sublime 的路径,因为您的版本可能不同。

subl . 

Should be working fine.

应该工作正常。

回答by EHLOVader

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 sublwas linked from the application directory into my ~/binper some followup instructions I found for brew cask. Unfortunately the path was never updated.

subl是从应用程序目录链接到我的~/bin每一些后续说明我发现酿造木桶。不幸的是,路径从未更新过。

回答by lpaglione

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.

遇到了同样的挑战,最终只是创建了一个 .bash_profile 文件,并直接在那里添加了路径语句。工作顺利。您可能还想查看 Alvin Alexander 的示例 .bash_profile 帖子(http://alvinalexander.com/blog/post/mac-os-x/sample-mac-osx-bashrc-terminal-startup-file) - 我发现了一个我还将添加一些其他有用的命令。

回答by Birkhoff Lee

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

以下是执行此操作的完整说明(适用于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 featureof 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'tbe able to modify the content of /usr/bin.

  2. Create a symlink from /usr/binto ~/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 .
    
  1. 请通过以下命令检查您的系统完整性保护状态:

    $ csrutil status
    

    如果已启用,请按照以下步骤操作:

    1. 通过重新启动机器并在启动时按住 Command + R 来引导到 OS X 的恢复操作系统。

    2. 从实用程序菜单启动终端。

    3. 运行以下命令:

      $ csrutil disable
      $ reboot
      

    这是由于OS X 的一项名为System Integrity Protection安全功能,可防止未经授权访问系统位置和进程。因此,如果启用此功能,您将无法修改 /usr/bin 的内容。

  2. 创建从/usr/bin~/bin的符号链接:

    $ sudo ln -s /usr/bin ~/bin
    
  3. 创建从 Sublime Text 3 CLI 工具到~/bin的符号链接:

    $ sudo ln -s /Applications/Sublime\ Text.app/Contents/SharedSupport/bin/subl ~/bin
    
  4. 测试一下。

    $ subl .