bash 你在 OSX 上在哪里保存你自己的脚本?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18682413/
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-10 00:10:01  来源:igfitidea点击:

Where do you keep your own scripts on OSX?

macosbashterminal

提问by More Than Five

As I write my bash scripts for my OS X that do general things, I am wondering where is a good place to keep them. Is there a directory I can put them all in where they will be picked up automatically? Or should I create my own directory and then reference this directory from .profile or something?

当我为我的 OS X 编写我的 bash 脚本来做一般事情时,我想知道在哪里保存它们的好地方。是否有一个目录可以将它们全部放在会自动拾取的位置?或者我应该创建我自己的目录,然后从 .profile 或其他东西引用这个目录?

回答by Paul R

Usually /usr/local/bin, unless you don't want other users to have access to them, in which case $HOME/bin.

通常/usr/local/bin,除非您不希望其他用户访问它们,在这种情况下$HOME/bin

/usr/local/binmay be in the default PATH, but $HOME/binwill certainly need to be added to PATH.

/usr/local/bin可能在默认的 PATH 中,但$HOME/bin肯定需要添加到 PATH 中。



Adding $HOME/binto PATH:添加$HOME/bin到路径:

PATH=${PATH}:$HOME/bin
export PATH

回答by David W.

I have my PATH set as:

我的 PATH 设置为:

  • /usr/local/bin
  • /usr/bin
  • /bin
  • /usr/sbin
  • /sbin
  • $HOME/bin
  • /usr/local/bin
  • /usr/bin
  • /bin
  • /usr/sbin
  • /sbin
  • $HOME/bin

I use /usr/local/binfor commands that I have that overridethe default commands. For example, I have Subversion 1.7.7 installed, and the OS X comes with 1.6.18. The version 1.7.7 of svnis in /usr/local/binwhile the default 1.6.18 version of svnis in /usr/bin. When I type svn, I get the version I installed instead of the version that comes with OS X. I've done this with Java, Git, Python, and several other binaries where I need a different version that what came on my Mac. Most of these are symbolic links. For example:

/usr/local/bin用于覆盖默认命令的命令。例如,我安装了 Subversion 1.7.7,而 OS X 带有 1.6.18。的 1.7.7 版本在svn中,/usr/local/bin而 的默认 1.6.18 版本svn在 中/usr/bin。当我输入 时svn,我得到的是我安装的版本,而不是 OS X 附带的版本。我已经使用 Java、Git、Python 和其他几个二进制文件完成了这项工作,我需要一个与 Mac 上不同的版本。其中大部分是符号链接。例如:

$ ls -l /usr/local/bin/ant
lrwxr-xr-x  1 root  wheel       16 Jun 12 11:01 ant -> /opt/ant/bin/ant

Ant 1.9.1 is installed in /opt/ant(actually, /opt/apache-ant-1.9.1, but it's symbolically linked to /opt/ant). I linked all the stuff under /opt/ant/binto /usr/local/bin, so it's in my path.

Ant 1.9.1 安装在/opt/ant(实际上,/opt/apache-ant-1.9.1,但它象征性地链接到/opt/ant)。我将所有内容链接/opt/ant/bin/usr/local/bin,因此它在我的路径中。

I use $HOME/binfor my personal shell scripts and other scripts. Traditionally, you make this the last entry in your PATH, so you don't accidentally override a built in command. If I made a shell script command called cp, I wouldn't override the /bin/cpcommand.

$HOME/bin用于我的个人 shell 脚本和其他脚本。传统上,您将其设为 PATH 中的最后一个条目,因此您不会意外覆盖内置命令。如果我创建了一个名为 的 shell 脚本命令cp,我就不会覆盖该/bin/cp命令。

回答by Lri

I use ~/binfor executables and scripts I wrote myself and /usr/local/binfor executables and scripts I didn't write myself.

~/bin用于我自己编写/usr/local/bin的可执行文件和脚本,以及我自己没有编写的可执行文件和脚本。

/usr/local/binis used by Homebrew, pip(and gemand npminstalled by Homebrew), as the default target for make install, and by some .pkginstallers.

/usr/local/bin通过使用自制,pip(以及gemnpm通过自制安装),对于缺省目标make install,并通过一些.pkg安装程序。

I used to have a separate directory for executables and scripts I didn't write myself and that weren't placed in /usr/local/binby default. But it contained so few files that I moved all files in it to /usr/local/bin.

我曾经有一个单独的目录,用于存放我没有自己编写的可执行文件和脚本,并且/usr/local/bin默认情况下也没有放在其中。但是它包含的文件很少,我将其中的所有文件都移到了/usr/local/bin.

I can find the non-Homebrew stuff in /usr/local/binwith find /usr/local/bin ! -lname '../Cellar/*'.

我可以找到非自制的东西/usr/local/binfind /usr/local/bin ! -lname '../Cellar/*'

I don't use /usr/local/binfor scripts I wrote myself, because /usr/local/binalready contains about 1000 other files, and ~/bin(or bin) is often easier to type.

我不/usr/local/bin用于我自己编写的脚本,因为/usr/local/bin已经包含大约 1000 个其他文件,并且~/bin(或bin) 通常更容易键入。

I have added setenv PATH ~/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/libexec:/usr/texbinto /etc/launchd.conf. Having /usr/local/binbefore the other directories is potentially dangerous, and for example some TextMate commands stopped working because of it, but it's also convenient to have newer versions of commands before the system-installed versions and to use the same path everywhere.

我已添加setenv PATH ~/bin:/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/libexec:/usr/texbin/etc/launchd.conf. 在/usr/local/bin其他目录之前有潜在的危险,例如一些 TextMate 命令因此停止工作,但在系统安装版本之前拥有更新版本的命令并在任何地方使用相同的路径也很方便。

回答by user1738671

In my mind, you can put your scripts where you want. It dosen't really matters unless you used system specefic folders. I do keep mine in /scr with root only permission on that folder. It's conviniant because all the script in this folder needs root access to perform correctly and I don't want end users to peek into that folder.

在我看来,你可以把你的脚本放在你想要的地方。除非您使用系统特定文件夹,否则这并不重要。我确实将我的保存在 /scr 中,该文件夹仅具有 root 权限。这是令人信服的,因为此文件夹中的所有脚本都需要 root 访问权限才能正确执行,而且我不希望最终用户窥视该文件夹。